Mudit
Mudit

Reputation: 69

unable to insert into database using jsp

I am trying to insert a query using the below jsp code

<%@ page import="java.sql.*, javax.servlet.http.*" language="java"    
contentType="text/html; charset=windows-1256" pageEncoding="windows-1256" %>


<%!public static Connection connect ()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection("jdbc:mysql://localhost/test","root","dhawanbhai1");
} 


catch (Exception e)
{
//throw new Error(e);
return null;

}

}

public static boolean close(Connection c)
{
try
{
c.close();
return true;

}

catch (Exception e)
{
return false;
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
/TR/html4/loose.dtd">
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Login Page</title>
</head> 
<body> 
<form action=""> 
Please enter your username 
<input type="text" name="un"/>
<br> 
Please enter your password
<input type="password" name="pw"/> 
<input type="submit" value="submit"> 
</form> 
</body> 
</html> 
<%
Connection c = connect();



String user=request.getParameter("un");
String pass=request.getParameter("pw");
PreparedStatement pst=c.prepareStatement("insert into userpass (id,password) 
values (?,?)");
pst.setString(1, user);
pst.setString(2, pass);


int val=pst.executeUpdate();

if(val>0)
System.out.println("Inserted record");
%>

but getting

org.apache.jasper.JasperException: An exception occurred processing JSP page 
/LoginPage.jsp at line 68

65: pst.setString(2, pass);
66: 
67: 
68: int val=pst.executeUpdate();
69: 
70: if(val>0)
71: System.out.println("Inserted record");


Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

root cause 

javax.servlet.ServletException: 
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 
'id' cannot be null

org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.LoginPage_jsp._jspService(LoginPage_jsp.java:147)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

Upvotes: 0

Views: 2085

Answers (1)

Vegard
Vegard

Reputation: 4497

In the stack trace it says that the Column 'id' cannot be null. So the problem is probably that request.getParameter("un"); is null. Check that you are passing the 'un' parameter and that you have the correct name 'un' in both places.

Change the bottom part of your code to look like this:

<%
String user=request.getParameter("un");
String pass=request.getParameter("pw");    
if (user != null && pass != null) {
    Connection c = connect();
    PreparedStatement pst=c.prepareStatement("insert into userpass (id,password) values (?,?)");
    pst.setString(1, user);
    pst.setString(2, pass);

    int val=pst.executeUpdate();

    if(val>0)
        System.out.println("Inserted record");
}
%>

When you first load your page 'un' and 'pw' will not be set, so it shouldn't be calling the insert method. This checks that the parameters are set before trying to insert it.

Upvotes: 2

Related Questions