详解JSP中的语句对象Statement操作MySQL的使用实例
语句对象Statement包含两个主要方法:executeUpdate()方法执行数据的更新操作(添加记录,删除记录,更新记录),executeQuery()方法用来执行数据的查询操作(查询记录)
添加记录
<%@pagelanguage="java"contentType="text/html;charset=gb2312"%>
<%@pageimport="java.sql.*"%>
<!DOCTYPEhtml>
<html>
<head>
<title>添加用户记录</title>
</head>
<body>
<%
Stringurl="jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
Stringuser="root";//登录数据库的用户名
Stringpassword="zhangda890126;;";//登录数据库的用户名的密码
Connectionconn=null;
try{
Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
conn=DriverManager.getConnection(url,user,password);//链接数据库
}catch(ClassNotFoundExceptione){
out.println("找不到驱动类");//抛出异常时,提示信息
}catch(SQLExceptione){
out.println("链接MySQL数据库失败");//处理SQLException异常
}
try{
//创建语句对象Statement
Statementstmt=conn.createStatement();
Stringadduser="INSERTINTOuser(userid,username,password)VALUES(null,'James','1234')";//添加用户
stmt.executeUpdate(adduser);//执行语句
}catch(SQLExceptione){
out.println("添加用户信息失败");
}
%>
</body>
</html>
<html>
<head>
<title>添加多个用户记录</title>
</head>
<body>
<%
Stringurl="jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
Stringuser="root";//登录数据库的用户名
Stringpassword="zhangda890126;;";//登录数据库的用户名的密码
Connectionconn=null;
try{
Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
conn=DriverManager.getConnection(url,user,password);//链接数据库
}catch(ClassNotFoundExceptione){
out.println("找不到驱动类");//抛出异常时,提示信息
}catch(SQLExceptione){
out.println("链接MySQL数据库失败");//处理SQLException异常
}
try{
//创建语句对象Statement
Statementstmt=conn.createStatement();
//删除userid为1的用户信息
for(inti=2;i<6;i++){
Stringusername="zhangda_"+i;
Stringadduser="INSERTINTOuser(userid,username,password)VALUES(null,'"+username+"','1234')";//添加用户
stmt.executeUpdate(adduser);//执行语句
}
}catch(SQLExceptione){
out.println("添加用户信息失败");
}
%>
</body>
</html>
更新记录
<%@pagelanguage="java"contentType="text/html;charset=gb2312"%>
<%@pageimport="java.sql.*"%>
<!DOCTYPEhtml>
<html>
<head>
<title>添加用户记录</title>
</head>
<body>
<%
Stringurl="jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
Stringuser="root";//登录数据库的用户名
Stringpassword="zhangda890126;;";//登录数据库的用户名的密码
Connectionconn=null;
try{
Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
conn=DriverManager.getConnection(url,user,password);//链接数据库
}catch(ClassNotFoundExceptione){
out.println("找不到驱动类");//抛出异常时,提示信息
}catch(SQLExceptione){
out.println("链接MySQL数据库失败");//处理SQLException异常
}
try{
//创建语句对象Statement
Statementstmt=conn.createStatement();
//更新userid为1的用户信息,更新其密码为12345
Stringupdateuser="UPDATEuserSETpassword='12345'WHEREuserid=1;";//添加用户
stmt.executeUpdate(updateuser);//执行语句
}catch(SQLExceptione){
out.println("更新用户信息失败");
}
%>
</body>
</html>
删除记录
<%@pagelanguage="java"contentType="text/html;charset=gb2312"%>
<%@pageimport="java.sql.*"%>
<!DOCTYPEhtml>
<html>
<head>
<title>添加用户记录</title>
</head>
<body>
<%
Stringurl="jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
Stringuser="root";//登录数据库的用户名
Stringpassword="zhangda890126;;";//登录数据库的用户名的密码
Connectionconn=null;
try{
Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
conn=DriverManager.getConnection(url,user,password);//链接数据库
}catch(ClassNotFoundExceptione){
out.println("找不到驱动类");//抛出异常时,提示信息
}catch(SQLExceptione){
out.println("链接MySQL数据库失败");//处理SQLException异常
}
try{
//创建语句对象Statement
Statementstmt=conn.createStatement();
//删除userid为1的用户信息
Stringdeleteuser="DELETEFROMuserWHEREuserid=1;";//添加用户
stmt.executeUpdate(deleteuser);//执行语句
}catch(SQLExceptione){
out.println("删除用户信息失败");
}
%>
</body>
</html>