
hi...
I have create a database connection using servletcontext . in this code when i login first time it will exceute sucessfully but when i use again the same page it will throw sql exception but i don't understand what is the problem ....
the code are as follows. the servlet context classss.
public void contextInitialized(ServletContextEvent arg0) {
ServletContext sc = arg0.getServletContext();
String url=sc.getInitParameter("url");
String user=sc.getInitParameter("username");
String pass= sc.getInitParameter("password");
database db = new database(url, user, pass);
System.out.println("hello*******************************************************************************");
sc.setAttribute("db", db);
}
//database connection class
Connection con = null;
public database(String url,String user, String pass) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
this.con = DriverManager.getConnection(url,user,pass);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("sorry********************"+e);
e.printStackTrace();
}
}
//the servlet class.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user= request.getParameter("username");
String password= request.getParameter("password");
database db = (database)getServletContext().getAttribute("db");
try {
PreparedStatement ps = db.con.prepareStatement("select * from admintable where vusername=? and vpassword=?");
ps.setString(1,user);
ps.setString(2, password);
ResultSet rs= ps.executeQuery();
if(rs.next())
{
System.out.println("************login sucessfully*******************");
PrintWriter out=response.getWriter();
out.println("hello arunnarwal");
db.con.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("sorry**************************");
e.printStackTrace();
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.