jsp---预处理语句对象PreparedStatement

本文提供了使用Java通过PreparedStatement对象操作MySQL数据库的示例代码,包括添加、更新、删除及查询数据等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

预处理语句对象PreparedStatement,使用PreparedStatement进行添加数据,更新数据,删除数据和查询数据

添加数据

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
	<head>
		<title>获得第二条记录开始的三条记录</title>
	</head>
	<body>
		<%
			String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
			String user = "root";//登录数据库的用户名
			String password = "zhangda890126;;";//登录数据库的用户名的密码
			Connection conn = null;//链接对象
			PreparedStatement pstmt = null;//语句对象
			//ResultSet rs = null;//结果集对象
			try{
				Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
				conn = DriverManager.getConnection(url,user,password);//链接数据库
			}catch(ClassNotFoundException e){
				out.println("找不到驱动类");//抛出异常时,提示信息
			}catch(SQLException e){
				out.println("链接MySQL数据库失败");//处理SQLException异常
			}
			try{
				String adduser = "INSERT INTO user (userid,username,password) VALUES(null,?,?)";//添加一条用户信息
				pstmt = conn.prepareStatement(adduser);//创建预处理语句对象PreparedStatement
				
				//设置参数
				pstmt.setString(1,"YAO");
				pstmt.setString(2,"yao");
				
				//执行语句
				pstmt.executeUpdate();
				
			}catch(SQLException e){
				out.println("添加用户信息失败");
			}
			
			try{
				if(pstmt != null){
					pstmt.close();
					conn = null;
				}
				if(conn != null){
					conn.close();
					conn = null;
				}
			}catch(Exception e){
			
				out.println("数据库关闭失败");
			}
		%>
	</body>
</html>

提示一下一定不要用错大小写,红色标记就是因为我前面的字母大写,花费了很长时间

更新数据

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
	<head>
		<title>获得第二条记录开始的三条记录</title>
	</head>
	<body>
		<%
			String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
			String user = "root";//登录数据库的用户名
			String password = "zhangda890126;;";//登录数据库的用户名的密码
			Connection conn = null;//链接对象
			PreparedStatement pstmt = null;//语句对象
			//ResultSet rs = null;//结果集对象
			try{
				Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
				conn = DriverManager.getConnection(url,user,password);//链接数据库
			}catch(ClassNotFoundException e){
				out.println("找不到驱动类");//抛出异常时,提示信息
			}catch(SQLException e){
				out.println("链接MySQL数据库失败");//处理SQLException异常
			}
			try{
				String updateuser = "UPDATE user SET password = ? WHERE userid = ?";//添加一条用户信息
				pstmt = conn.prepareStatement(updateuser);//创建预处理语句对象PreparedStatement
				
				//设置参数
				pstmt.setString(1,"hello world");
				pstmt.setInt(2,1);
				
				//执行语句
				pstmt.executeUpdate();
				
			}catch(SQLException e){
				out.println("添加用户信息失败");
			}
			
			try{
				if(pstmt != null){
					pstmt.close();
					conn = null;
				}
				if(conn != null){
					conn.close();
					conn = null;
				}
			}catch(Exception e){
			
				out.println("数据库关闭失败");
			}
		%>
	</body>
</html>

删除数据

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
	<head>
		<title>获得第二条记录开始的三条记录</title>
	</head>
	<body>
		<%
			String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
			String user = "root";//登录数据库的用户名
			String password = "zhangda890126;;";//登录数据库的用户名的密码
			Connection conn = null;//链接对象
			PreparedStatement pstmt = null;//语句对象
			//ResultSet rs = null;//结果集对象
			try{
				Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
				conn = DriverManager.getConnection(url,user,password);//链接数据库
			}catch(ClassNotFoundException e){
				out.println("找不到驱动类");//抛出异常时,提示信息
			}catch(SQLException e){
				out.println("链接MySQL数据库失败");//处理SQLException异常
			}
			try{
				String deleteuser = "DELETE FROM user WHERE userid = ?";//添加一条用户信息
				pstmt = conn.prepareStatement(deleteuser);//创建预处理语句对象PreparedStatement
				
				//设置参数
				pstmt.setInt(1,2);
				
				//执行语句
				pstmt.executeUpdate();
				
			}catch(SQLException e){
				out.println("添加用户信息失败");
			}
			
			try{
				if(pstmt != null){
					pstmt.close();
					conn = null;
				}
				if(conn != null){
					conn.close();
					conn = null;
				}
			}catch(Exception e){
			
				out.println("数据库关闭失败");
			}
		%>
	</body>
</html>

查询数据

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
	<head>
		<title>获得第二条记录开始的三条记录</title>
	</head>
	<body>
		<%
			String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
			String user = "root";//登录数据库的用户名
			String password = "zhangda890126;;";//登录数据库的用户名的密码
			Connection conn = null;//链接对象
			PreparedStatement pstmt = null;//语句对象
			ResultSet rs = null;//结果集对象
			try{
				Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
				conn = DriverManager.getConnection(url,user,password);//链接数据库
			}catch(ClassNotFoundException e){
				out.println("找不到驱动类");//抛出异常时,提示信息
			}catch(SQLException e){
				out.println("链接MySQL数据库失败");//处理SQLException异常
			}
			try{
				String queryAll = "SELECT * FROM user LIMIT ?,?;";//添加一条用户信息
				pstmt = conn.prepareStatement(queryAll);//创建预处理语句对象PreparedStatement
				
				//设置参数
				pstmt.setInt(1,2);
				pstmt.setInt(2,5);
				
				//执行语句
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					int userid = rs.getInt(1);
					String username = rs.getString(2);
					String userpassword = rs.getString(3);
					out.println("用户的ID:"+userid+"用户名:"+username+"用户的密码:"+userpassword+"<br />");
				}
				
			}catch(SQLException e){
				out.println("添加用户信息失败");
			}
			
			try{
				if(pstmt != null){
					pstmt.close();
					conn = null;
				}
				if(conn != null){
					conn.close();
					conn = null;
				}
			}catch(Exception e){
			
				out.println("数据库关闭失败");
			}
		%>
	</body>
</html>

转载于:https://my.oschina.net/zhangdapeng89/blog/39884

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值