控制台输出为“插入数据失败1986291198nullnull”,请检查代码 <% String content = request.getParameter("area"); String score = request.getParameter("sor"); Connection conn = SQLConn.openDB(); %> <% //创建Statement , 执行 SQL 查询 Statement sm=conn.createStatement(); //查询用户名登陆状态 String sql; sql ="select UserId,Name,Login from User_ZC where Login='"+1+"' "; // 执行查询 ResultSet rs = sm.executeQuery(sql); if(rs.next()) { //用户在登陆状态 String name = rs.getString("Name"); int ID = rs.getInt("UserId"); int dbpass = rs.getInt(1); //System.out.println(dbpass); if(dbpass>0){ //用户合法,可以进行评论 final String INSERT_SQL = "INSERT INTO LiuYan(UserId, Score, Word) VALUES (?, ?, ?)"; //向评论表中插入数据 int count = 0; try { PreparedStatement stmt = conn.prepareStatement(INSERT_SQL); { stmt.setLong(1, ID); stmt.setString(2, score); stmt.setString(3, content); count = stmt.executeUpdate(); } }catch (SQLException e) { e.printStackTrace(); } if(count>0){ //插入成功,在页面显示刚才输入的评论 %> 用户名:<%=name%> 满意度:<%=score %> 评论:<%=content %> <% System.out.print("评论存入表LiuYan成功"); }else{ %> 插入出错 <% System.out.print("插入数据失败"); System.out.print(ID); System.out.print(score); System.out.print(content); } }else{ System.out.print("未登录,请先登陆再进行预约"); request.getRequestDispatcher("ZCB.jsp").forward(request, response); }} else{ System.out.print("未登录,请先登陆再进行预约"); request.getRequestDispatcher("ZCB.jsp").forward(request, response); } %>
时间: 2024-03-03 14:50:37 浏览: 206
JS笔记大全.docx
根据你提供的信息,错误信息中包含了 "nullnull",说明score或content可能为null。你可以在插入数据之前判断一下score和content是否为null或空字符串,如果是,则不执行插入操作,直接返回错误信息。修改后的代码如下所示:
```
if (score == null || content == null || score.trim().equals("") || content.trim().equals("")) {
System.out.print("提交的评分或内容为空");
} else {
final String INSERT_SQL = "INSERT INTO LiuYan(UserId, Score, Word) VALUES (?, ?, ?)";
int count = 0;
try {
PreparedStatement stmt = conn.prepareStatement(INSERT_SQL);
{
stmt.setLong(1, ID);
stmt.setString(2, score);
stmt.setString(3, content);
count = stmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
if (count > 0) {
//插入成功,在页面显示刚才输入的评论
%>
用户名:<%=name%>
满意度:<%=score %>
评论:<%=content %>
<%
System.out.print("评论存入表LiuYan成功");
} else {
%>
插入出错
<%
System.out.print("插入数据失败");
System.out.print(ID);
System.out.print(score);
System.out.print(content);
}
}
```
这样可以避免插入数据时出现空指针异常或插入空数据的情况。
阅读全文