版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41282486/article/details/80703851
PreparedStatement 的执行效率高于Statement对象,对于需要多次执行的SQL语句经常使用PreparedStatement 。
与Statement的主要区别在于采用?的占位符。
PreparedStatement 除了继承Statement 的所有操作外还增加了一些新的操作。
使用PrepareStatement完成数据的插入操作:
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.PreparedStatement ;
import java.text.SimpleDateFormat ;
public class PreparedStatementDemo01 {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "admin" ;
public static void main(String args[]) throws Exception{ // 所有异常抛出
Connection conn = null ; // 数据库连接
PreparedStatement pstmt = null ; // 数据库操作
String name = "李兴华" ; // 姓名
String password = "www.mldnjava.cn" ; // 密码
int age = 30 ; // 年龄
String sex = "男" ; // 性别
String birthday = "2007-08-27" ; // 生日
java.util.Date temp = null ;
temp = new SimpleDateFormat("yyyy-MM-dd").parse(birthday) ;
java.sql.Date bir = new java.sql.Date(temp.getTime()) ;
String sql = "INSERT INTO user(name,password,age,sex,birthday) VALUES (?,?,?,?,?) " ;
Class.forName(DBDRIVER) ; // 加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ; // 实例化PreapredStatement对象
pstmt.setString(1,name) ;
pstmt.setString(2,password) ;
pstmt.setInt(3,age) ;
pstmt.setString(4,sex) ;
pstmt.setDate(5,bir) ;
int t = pstmt.executeUpdate() ; // 执行更新
System.out.println(t);
pstmt.close() ;
conn.close() ; // 数据库关闭
}
};
使用PreparedStatement 完成模糊查询:
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.ResultSet ;
import java.sql.PreparedStatement ;
import java.text.SimpleDateFormat ;
public class PreparedStatementDemo02 {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "admin" ;
public static void main(String args[]) throws Exception{ // 所有异常抛出
Connection conn = null ; // 数据库连接
PreparedStatement pstmt = null ; // 数据库操作
String keyWord = "李" ; // 设置查询关键字
ResultSet rs = null ; // 接收查询结果
String sql = "SELECT id,name,password,age,sex,birthday " +
" FROM user WHERE name LIKE ? OR password LIKE ? OR sex LIKE ?" ;
Class.forName(DBDRIVER) ; // 加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ; // 实例化PreapredStatement对象
pstmt.setString(1,"%"+keyWord+"%") ;
pstmt.setString(2,"%"+keyWord+"%") ;
pstmt.setString(3,"%"+keyWord+"%") ;
rs = pstmt.executeQuery() ; // 执行查询
while(rs.next()){
int id = rs.getInt(1) ;
String name = rs.getString(2) ;
String pass = rs.getString(3) ;
int age = rs.getInt(4) ;
String sex = rs.getString(5) ;
java.util.Date d = rs.getDate(6) ;
System.out.print("编号:" + id + ";") ;
System.out.print("姓名:" + name + ";") ;
System.out.print("密码:" + pass + ";") ;
System.out.print("年龄:" + age + ";") ;
System.out.print("性别:" + sex + ";") ;
System.out.println("生日:" + d + ";") ;
System.out.println("-------------------------") ;
}
rs.close() ;
pstmt.close() ;
conn.close() ; // 数据库关闭
}
};