JSP中的PreparedStatement对象操作数据库的使用教程
|
PreparedStatement接口继承Statement,并与之在两方面有所不同: pstmt.setLong(1,123456789); pstmt.setLong(2,100000000); 一旦设置了给定语句的参数值,就可用它多次执行该语句,直到调用clearParameters 方法清除它为止。在连接的缺省模式下(启用自动提交),当语句完成时将自动提交或还原该语句。
pstmt.setString(1,"Hi");
for (int i = 0; i < 10; i++) {
pstmt.setInt(2,i);
int rowCount = pstmt.executeUpdate();
}
3、IN 参数中数据类型的一致性 实例:
<%@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.<span style="color:#e53333;"><b>prepareStatement</b></span>(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>
提示一下一定不要用错大小写,红色标记就是因为我前面的字母大写,花费了很长时间 2.更新数据
<%@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,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>
3.删除数据
<%@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,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>
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
