the method preparement(String) is undefined for the type SQLConn
时间: 2024-05-18 21:18:45 浏览: 13
抱歉,我的回答中有误。应该将SQLConn类中的openDB方法改为返回Connection对象,然后在JSP页面中使用该Connection对象创建PreparedStatement对象。
具体修改如下:
SQLConn.java:
public class SQLConn {
// 省略其他代码
// 修改openDB方法
public static Connection openDB() throws ClassNotFoundException, SQLException {
// 加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
// 创建数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
// 返回连接对象
return conn;
}
// 省略其他代码
}
login.jsp:
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="utf-8"%>
<%@ 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 from User_ZC where usernum=? and userpwd=?");
pstmt.setString(1, usernum);
pstmt.setString(2, userpwd);
ResultSet rs = pstmt.executeQuery();
boolean v = rs.next();
rs.close();
pstmt.close();
conn.close();
if(v){
request.getRequestDispatcher("ZCS.jsp").forward(request, response);
}else{
request.getRequestDispatcher("ZCB.jsp").forward(request, response);
}
%>
</body>
</html>
在JSP页面中,我们使用SQLConn类的openDB方法获取数据库连接,然后使用该连接对象创建PreparedStatement对象,设置参数并执行查询。在使用完PreparedStatement和ResultSet之后,及时关闭资源。
再次抱歉给您带来的困扰。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<% String code = String.valueOf(request.getParameter("YYM")); System.out.print(code); if (code == null || code.isEmpty()) { %> 请输入预约码,点击返回签到 <% } else { Connection conn = SQLConn.openDB(); final String SELECT_SQL = "SELECT ACode FROM User_ZC JOIN YuYue ON User_ZC.UserId = YuYue.UserId WHERE Login = 1 AND State = 1 AND Arrive IS NULL"; final String UPDATE_SQL = "UPDATE YuYue SET Arrive =1 WHERE ACode = ?"; PreparedStatement pstmtSelect = conn.prepareStatement(SELECT_SQL); ResultSet rs = pstmtSelect.executeQuery(); int count = 0; while (rs.next()) { String Acode = rs.getString("ACode"); if (code==Acode) { try { PreparedStatement pstmtUpdate = conn.prepareStatement(UPDATE_SQL); pstmtUpdate.setString(1, code); count = pstmtUpdate.executeUpdate(); pstmtUpdate.close(); } catch (SQLException e) { e.printStackTrace(); } break; }else{ System.out.print("预约码不匹配"); } } if (count > 0){ %> 签到成功 <% } else { %> 签到失败 <% } rs.close(); pstmtSelect.close(); conn.close(); } %>为什么输入正确的YYM,也会提示“预约码不匹配”
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<% String code = request.getParameter("YYM"); if (code == null || code.isEmpty()) { %> 请输入预约码,点击返回签到 <% } else { Connection conn = SQLConn.openDB(); final String SELECT_SQL = "SELECT ACode FROM User_ZC JOIN YuYue ON User_ZC.UserId = YuYue.UserId WHERE Login = 1 AND State = 1 AND Arrive IS NULL"; final String UPDATE_SQL = "UPDATE YuYue SET Arrive =1 WHERE ACode = ?"; PreparedStatement pstmtSelect = conn.prepareStatement(SELECT_SQL); ResultSet rs = pstmtSelect.executeQuery(); int count = 0; while (rs.next()) { String Acode = rs.getString("ACode"); if (Acode.equals(code)) { try { PreparedStatement pstmtUpdate = conn.prepareStatement(UPDATE_SQL); pstmtUpdate.setString(1, code); count = pstmtUpdate.executeUpdate(); pstmtUpdate.close(); } catch (SQLException e) { e.printStackTrace(); } break; } } if (count > 0){ %> 签到成功 <% } else { %> 签到失败 <% } rs.close(); pstmtSelect.close(); conn.close(); } %>为什么总是跳转到“签到失败”页面,请找出bug
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8"); System.out.print(name); if (name == null || name.isEmpty()) { %> 请输入中药名称,点击返回重新输入 <% }else{ Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = SQLConn.openDB(); stmt = conn.createStatement(); // 执行查询等操作 String sql = "select ZYName,YLName,YDY,YX,GX,ZhongYao.ZZ as ZZ from Yaolei,ZhongYao where Yaolei.YLID=ZhongYao.YLID"; PreparedStatement pstmt = conn.prepareStatement(sql); //pstmt.setString(1, "%" + name + "%"); rs = pstmt.executeQuery(); int count = 0; if(rs.next()){ while (rs.next()) { String ZYName = rs.getString("ZYName"); System.out.print(name); if (name.trim().equals(ZYName.trim())) { // if (name.trim().equals(A.trim())) { %>为什么name.trim().equals(ZYName.trim())不成立
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)