<%@ 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中运行此代码时,控制台上都会显示以下消息,但是我的数据库得到了更新
net.ucanaccess.jdbc.UcanaccessSQLException: integrity constraint violation: NOT NULL check constraint; SYS_PK_10174 table: USERDETAILS column: NAME
最佳答案
您将在提交表单之前在页面加载时插入数据库。因此,参数为null
。您应该将action
属性设置为另一个页面或更好的servlet,该页面应通过执行Java和sql代码执行更新,然后重定向到视图页面。您不需要将null
值插入数据库中,因为启用了约束以不允许NULL
值。
<form action="yourpage.jsp" method="post" >