jsp---语句对象Statement

语句对象Statement包含两个主要方法:executeUpdate()方法执行数据的更新操作(添加记录,删除记录,更新记录),executeQuery()方法用来执行数据的查询操作(查询记录)

添加记录

<%@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;
			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{
				//创建语句对象Statement
				Statement stmt = conn.createStatement();
				
				String adduser = "INSERT INTO user(userid,username,password) VALUES (null,'James','1234')";//添加用户
				
				stmt.executeUpdate(adduser);//执行语句
			}catch(SQLException e){
				out.println("添加用户信息失败");
			}
		%>
	</body>
</html>

<html>
	<head>
		<title>添加多个用户记录</title>
	</head>
	<body>
		<%
			String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
			String user = "root";//登录数据库的用户名
			String password = "zhangda890126;;";//登录数据库的用户名的密码
			Connection conn = 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{
				//创建语句对象Statement
				Statement stmt = conn.createStatement();
				//删除userid为1的用户信息
				for(int i=2;i<6;i++){
					String username = "zhangda_"+i;
					String adduser = "INSERT INTO user (userid,username,password) VALUES (null,'"+username+"','1234')";//添加用户
					stmt.executeUpdate(adduser);//执行语句
				}
			}catch(SQLException 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;
			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{
				//创建语句对象Statement
				Statement stmt = conn.createStatement();
				//更新userid为1的用户信息,更新其密码为12345
				String updateuser = "UPDATE user SET password='12345' WHERE userid=1;";//添加用户
				
				stmt.executeUpdate(updateuser);//执行语句
			}catch(SQLException 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;
			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{
				//创建语句对象Statement
				Statement stmt = conn.createStatement();
				//删除userid为1的用户信息
				String deleteuser = "DELETE FROM user WHERE userid=1;";//添加用户
				
				stmt.executeUpdate(deleteuser);//执行语句
			}catch(SQLException e){
				out.println("删除用户信息失败");
			}
		%>
	</body>
</html>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值