У меня есть класс для формирования соединения с БД
public class MyConnection {
private static Connection con;
private static String url = "jdbc:sqlserver://localhost:1433;databaseName =FininceAnalysis";
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String user = "Admin";
private static String password = "1234";
public MyConnection(){}
public static Connection openConnection() throws ClassNotFoundException,SQLException{
if(con==null){
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
}
return con;
}
public static void closeConnection()throws SQLException{
if(con!=null){
con.close();
}
con = null;
} }
Он вызывается в других сервлетах для установления соединения с БД Сервлеты такого типа
@WebServlet(name = "DayIndex", urlPatterns = {"/DayIndex"})public class DayIndex extends HttpServlet {public String getTable()throws Exception{
String sql = "SELECT [dbo].[Report_Count_Index].[ID_Index], [dbo].[Report_Count_Index].Data, [dbo].[Index_Table].Name,[dbo].[Report_Count_Index].[Varible],[dbo].[Index_Table].Max,[dbo].[Index_Table].Min,[dbo].[Index_Table].Description, [dbo].[Report_Count_Index].Recomendation, [dbo].[Report_Count_Index].Day_of_week,[dbo].[Report_Count_Index].Day_of_Mounth,[dbo].[Report_Count_Index].Day_of_Year,[dbo].[Report_Count_Index].Name_Mounth,[dbo].[Report_Count_Index].Number_Mounth,[dbo].[Report_Count_Index].Quater_Current,[dbo].[Report_Count_Index].Week_of_year,[dbo].[Report_Count_Index].Year_Current, [dbo].[Type_Dim].Name as 'Index_Kind', [dbo].[Sorce_Dim].Name as 'Sorce', [dbo].[Unit_Dim].Name as 'Unit',[dbo].[Sorce_Dim].Code FROM [dbo].[Report_Count_Index] INNER JOIN [dbo].[Index_Table] ON [dbo].[Report_Count_Index].[ID_Index]=[dbo].[Index_Table].[ID_Index] INNER JOIN [dbo].[Type_Dim] ON [dbo].Type_Dim.ID_Type=[dbo].[Index_Table].ID_Type INNER JOIN [dbo].[Unit_Dim] ON [dbo].[Unit_Dim].ID_Unit=[dbo].[Index_Table].ID_Unit INNER JOIN [dbo].[Sorce_Dim] ON [dbo].[Sorce_Dim].ID_Sorce=[dbo].[Index_Table].ID_Unit WHERE [dbo].[Report_Count_Index].Data>=DATEADD(DAY,-1, CONVERT(CHAR(8), GETDATE(),112)) AND [dbo].[Report_Count_Index].Data<CONVERT(CHAR(8), GETDATE(),112) ORDER BY [dbo].[Report_Count_Index].[ID_Index] ";
Statement st = MyConnection.openConnection().createStatement();
ResultSet rs = st.executeQuery(sql);
ArrayList<IndexReportMethods>dataTableOne = new ArrayList<IndexReportMethods>();
while (rs.next()) {
dataTableOne.add(new IndexReportMethods(rs.getInt("ID_Index"),
rs.getString("Data"),rs.getString("Name"),rs.getFloat("Varible"),rs.getFloat("Max"),
rs.getFloat("Min"), rs.getString("Description"),rs.getString("Recomendation"),
rs.getString("Day_of_week"), rs.getInt("Day_of_Mounth"), rs.getInt("Day_of_Year"),
rs.getString("Name_Mounth"), rs.getInt("Number_Mounth"), rs.getInt("Quater_Current"),
rs.getInt("Week_of_year"), rs.getInt("Year_Current"), rs.getString("Index_Kind"),
rs.getString("Sorce"), rs.getString("Unit"), rs.getString("Code")
));
}
return new Gson().toJson(dataTableOne);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
out.println(getTable());
}
catch(Exception e){
out.println("Error"+e.getMessage());
}finally{
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}}
Необходимо создать сервлет для авторизации в БД, т.е ввожу логин и пароль на форме и происходит установка соединения с БД, и другие сервлеты должны устанавливать соединение по этим параметрам. Возможно ли это вообще? Заранее спасибо