java - 无法使用jsp插入数据库

标签 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)

最佳答案

在堆栈跟踪中,它表明列“id”不能为空。所以问题可能是 request.getParameter("un"); 为 null。检查您是否传递了“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”不会被设置,因此它不应该调用插入方法。这会在尝试插入之前检查参数是否已设置。

关于java - 无法使用jsp插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440112/

相关文章:

mysql - 从 3 个表中获取所有结果而不重复

java - 此代码未提交表单?为什么

java - 从 OpenShift 服务器获取图像路径。 Spring MVC。联合应用程序

java - 在一个 JOptionPane 消息框中转换一堆 System.out.prints

java - 为什么实例变量在调用构造函数之前被初始化?

java - Android:如何在不删除底部的 splitactionbar 的情况下删除标题栏?

php - ajax调用php脚本没有错误没有记录

php mysql语法错误

java - 排行榜配置错误

java - 将 java bean 实现为 jsp servlet