PreparedStatement-预处理语句的用法

原理:更新或插入时先占位,再设置占位的字段;
查询时,若不是模糊查询,和Statement一样,不变set方法,若是模糊查询,必须定义模糊?的意义!
—–实现基本的查询:
package com.mldn;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Prepared
{ // 连接驱动路径
public static final String DRIVER = “com.mysql.jdbc.Driver”; // “org.gjt.mm.mysql.driver”
// 定义数据源URL:数据库地址
public static final String DBURL = “jdbc:mysql://localhost:3306/school?characterEncoding=utf8”;
// 定义数据库登录的用户名:
public static final String USERNAME = “root”;
// 定义数据库登录的密码:
public static final String PASSWORD = “123456”;
public static void main(String[] args) throws Exception // 抛出所有异常
{
Class.forName(DRIVER); // 加载驱动
Connection conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD); // 获取连接
String sql = “select id, name, age, sex, birthday from student”; // 精确查询
PreparedStatement ptmt = conn.prepareStatement(sql); // 获取预编译对象
ResultSet rs = ptmt.executeQuery(); // 获取查询结果
while(rs.next())
{
int id = rs.getInt(“id”); // 获取id
String name = rs.getString(“name”); // 获取name
int age = rs.getInt(“age”); // 获取年龄
String sex = rs.getString(“sex”); // 获取性别
java.util.Date date = rs.getDate(“birthday”); // 获取生日
System.out.print(“id: ” + id + ” ” + “姓名:” + name + ” ” + “年龄:” + age + ” ”
+ “性别:” + sex + ” ” + “生日:” + date + “\n”);
}
rs.close(); // 关闭操作
ptmt.close();
conn.close(); // 关闭连接
}
}
/*
ubuntu@xu-desktop:~$ javac -d . work/Prepared.java
ubuntu@xu-desktop:~$ java com.mldn.Prepared
id: 1 姓名:王敏 年龄:20 性别:女 生日:2009-01-05
id: 14 姓名:梨花 年龄:23 性别:女 生日:1991-02-08
id: 3 姓名:王方 年龄:31 性别:女 生日:2009-01-05
id: 4 姓名:李林 年龄:23 性别:女 生日:2009-01-05
id: 5 姓名:张速 年龄:21 性别:男 生日:2009-01-05
id: 6 姓名:赵凌 年龄:25 性别:女 生日:2009-01-05
// 基本的精确查询和Statement一样,但PreparedStatement更常用,因为支持指定条件的模糊查询!,特别是对更新和插入的支持,更灵活!
*/
——–执行自定义插入操作:
package com.mldn;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
public class Prepared
{ // 连接驱动路径
public static final String DRIVER = “com.mysql.jdbc.Driver”; // “org.gjt.mm.mysql.driver”
// 定义数据源URL:数据库地址
public static final String DBURL = “jdbc:mysql://localhost:3306/school?characterEncoding=utf8”;
// 定义数据库登录的用户名:
public static final String USERNAME = “root”;
// 定义数据库登录的密码:
public static final String PASSWORD = “123456”;
public static void main(String[] args) throws Exception // 抛出所有异常
{
Connection conn = null; // 数据库连接
PreparedStatement pstmt = null; // 预处理语句
// 变量接口,和GUI关联:
String name = “k187”;
int age = 29;
String sex = “男”;
String birth = “1982-02-25”; // 给定字符串日期
java.util.Date temp = null;
java.sql.Date birthday = null;
temp = new SimpleDateFormat(“yyyy-MM-dd”).parse(birth); // 字符串转换为java.util.Date类型,
birthday = new java.sql.Date(temp.getTime()); // setDate方法使用的是java.sql.Date类型,是前者的子类
Class.forName(DRIVER); // 加载驱动
conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD); // 获取连接
// 插入操作
String sql = “INSERT INTO student(name, age, sex, birthday) VALUES(?, ?, ?, ?)”;
// 获取预编译对象
pstmt = conn.prepareStatement(sql);
// 设置字段:
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, sex);
pstmt.setDate(4, birthday); // java.sql.Date
// 执行插入操作
pstmt.executeUpdate();
// 关闭所有操作和连接!
pstmt.close();
conn.close();
}
}
/*
ubuntu@xu-desktop:~$ javac -d . work/Prepared.java
ubuntu@xu-desktop:~$ java com.mldn.Prepared
ubuntu@xu-desktop:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.1.37-1ubuntu5.1 (Ubuntu)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> use school
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select id, name, age, sex, birthday from student where name = ‘k187’ ;
+—-+——+—–+——+————+
| id | name | age | sex | birthday |
+—-+——+—–+——+————+
| 25 | k187 | 29 | 男 | 1982-02-25 |
+—-+——+—–+——+————+
1 row in set (0.00 sec)
// PreparedStatement可以方便的自定义插入操作
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《PreparedStatement-预处理语句的用法》
本文地址:http://www.xiupu.net/archives-153.html
关注公众号:
微信赞赏
支付宝赞赏
