语句对象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>
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!