<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="utf-8"%> <%@page import="com.util.SQLConn"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陆跳转servlet</title> </head> <body> <% String usernum = request.getParameter("usernum"); String userpwd = request.getParameter("userpwd"); Connection conn = SQLConn.openDB(); // 使用PreparedStatment进行预编译 PreparedStatement pstmt = conn.prepareStatement("select UserId, Code,Login from User_ZC where UserId=? and Code=? and Login=0"); pstmt.setString(1, usernum); pstmt.setString(2, userpwd); ResultSet rs = pstmt.executeQuery(); boolean v = rs.next(); if(v){ final String UPDATE_SQL = "update User_ZC set Login=? where UserId=?"; int count = 0; try { Connection connn = SQLConn.openDB(); PreparedStatement pstmte = conn.prepareStatement(UPDATE_SQL); { pstmt.setInt(1, 1); pstmt.setString(2, usernum); count = pstmt.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } if (count > 0){ request.getRequestDispatcher("ZCS.jsp").forward(request, response); }else{System.out.print("登陆失败");} } else{ request.getRequestDispatcher("ZCB.jsp").forward(request, response); } %> </body> </html>找bug
时间: 2024-01-17 22:02:43 浏览: 68
基于Java的制作简单的表单程序(Jsp).zip
在第24行,应该使用已经预编译好的 PreparedStatement 对象 pstmte 而不是重新使用 conn.prepareStatement() 方法创建一个新的 PreparedStatement 对象。正确的代码应该是:
```
pstmte.setInt(1, 1);
pstmte.setString(2, usernum);
count = pstmte.executeUpdate();
```
阅读全文