无法使用jsp插入数据库

时间:2013-12-07 10:10:06

标签: java mysql jsp

我正在尝试使用以下jsp代码插入查询

<%@ 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");
%>

但是

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)

1 个答案:

答案 0 :(得分:2)

在堆栈跟踪中,它表示Column'id'不能为null。所以问题可能是request.getParameter("un");为空。检查您是否传递了'un'参数,并且在两个地方都有正确的名称'un'。

将代码的底部部分更改为:

<%
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");
}
%>

当您第一次加载页面时,“un”和“pw”将不会被设置,因此它不应该调用insert方法。这会在尝试插入参数之前检查参数是否已设置。