【发布时间】:2013-12-02 22:38:47
【问题描述】:
我的项目是教师分配。当我尝试插入数据时,它显示以下异常。
"com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Column 'hourId' cannot be null"
谁能帮我解决这个问题? 请提供示例代码以避免此异常。
我的编码是
<%
Connection con = null;
String StaffName = request.getParameter("StaffName");
// String subcode = request.getParameter("subcode");
String hourId = request.getParameter("hourId");
String daysId = request.getParameter("daysId");
String date = request.getParameter("date");
//String queryText = "insert into tblstaffallocation (StaffName, subcode,hourId, daysId, date) values('"+StaffName+"','"+subcode+"','+hourId+','+daysId+','+date+')";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation","root","success");
// PreparedStatement stat = con.PrepareStatement();
String updateString ="INSERT INTO tblstaffallocation (StaffName,hourId,daysId,date) VALUES (?,?,?,?)";
PreparedStatement preparedStatement = con.prepareStatement(updateString);
preparedStatement.setString(1, StaffName);
preparedStatement.setString(2, hourId);
preparedStatement.setString(3, daysId);
preparedStatement.setString(4, date);
preparedStatement.executeUpdate();
//int rst = stat.executeUpdate("insert into tblstaffallocation ('StaffName','hourId', 'daysId', 'date') values('"+StaffName+"','"+hourId+"','"+daysId+"','"+date+"')");
%>
<table cellpadding="4" border="1" cellspacing="4" align="center">
<th>StaffName</th><th>hourId</th><th>daysId</th><th>date</th>
<%
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from tblstaffallocation");
while(rs.next()){
rs.getString(1);
rs.getString(2);
rs.getString(3);
rs.getString(4);
}
} catch (Exception e) {
out.print(e);
}
【问题讨论】:
-
将 column_name 更改为可为空
-
我试过了,当我将数据插入表中时,编码工作。但是当我从 tblstaffallocation 中选择 * 时。除人员名称外,所有输入的值都存储为空值。
-
@user2951465 hourid 列不接受 null。确保 request.getParameter("hourId");不返回 null
-
@lucas 我不明白你,我应该提供什么名字?请更具体
-
@user2951465 抱歉,我的意思是 hourId :)