11.22判断是否存在

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.*" %>
<html>
<body>

<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet existingRecord = null;
String message = "";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/demo?useSSL=false";
String name1 = "root";
String password = "123456";
conn = DriverManager.getConnection(url, name1, password);
String query = "SELECT * FROM teacher WHERE id=?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, id);
existingRecord = pstmt.executeQuery();

if (existingRecord.next()) {
response.sendRedirect("teacher2.jsp");

} else {
out.println("编号 " + id + " 不存在。");
}

} catch (SQLException | ClassNotFoundException e) {
System.out.println("发生错误: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (existingRecord != null) {
existingRecord.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
System.out.println("关闭资源时发生错误: " + e.getMessage());
}
}
if (message.equals("插入成功!")) {

}
%>


</body>
</html>

posted @   new菜鸟  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示