2014-10-02 123 views
3
<%@ page import="java.sql.*" %> 
<HTML> 
<BODY> 
<form method="post" > 

    Name:<input type="text" name="userName"><br> 
    Password:<input type="password" name="password"><br> 
    Email Id:<input type="text" name="email"><br> 
    Language: <select name="language"> 
     <option>Hindi</option> 
     <option>English</option> 
     <option>French</option> 
    </select> <br> 
    <input type="submit" value="Submit"> 

</form> 


<% 
String n = request.getParameter("userName"); 
String p = request.getParameter("password"); 
String e = request.getParameter("email"); 
String c = request.getParameter("language"); 


try { 

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); 

Connection con=  DriverManager.getConnection("jdbc:ucanaccess://D:/eclipse/register.accdb","",""); 

PreparedStatement ps = con 
     .prepareStatement("insert into USERDETAILS values(?,?,?,?)"); 

ps.setString(1, n); 
ps.setString(2, p); 
ps.setString(3, e); 
ps.setString(4, c); 

int i= ps.executeUpdate(); 
if (i > 0) { 
    out.print("You are successfully registered..."); 
} 

else{ 
    out.println("failed"); 
} 


} catch (Exception e2) { 
System.out.println(e2); 
} 
%> 

</BODY> 
</html> 

每次我在Eclipse中運行該代碼出現在控制檯下面的消息,但我的數據庫得到更新MS ACCESS完整性約束違規:NOT NULL檢查約束;

net.ucanaccess.jdbc.UcanaccessSQLException: integrity constraint violation: NOT NULL check constraint; SYS_PK_10174 table: USERDETAILS column: NAME 

回答

1

提交表單之前要插入到數據庫的頁面加載。因此參數是null。您應該將action屬性設置爲另一個頁面或更好的servlet,它們應該通過執行java和sql代碼執行更新,然後重定向到視圖頁面。您無需將null值插入數據庫,因爲啓用了約束條件以不允許NULL值。

<form action="yourpage.jsp" method="post" > 
相關問題