簡體   English   中英

無法使用jsp插入數據庫

[英]unable to insert into database using 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)

在堆棧跟蹤中,它表示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方法。 這會在嘗試插入參數之前檢查參數是否已設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM