使用PreparedStatement 操作数据表,完成增删改查操作。
1.通过JdbcUtils中的连接方法获取连接
conn = JdbcUtils.getConnection();
2.预编译sql语句,返回PreparedStatement的实例
- String sql = "insert into customers(name,email)values(?,?)";// ? 是占位符
- ps = conn.prepareStatement(sql);
3.填充占位符:输入需要添加/修改/删除的值
- ps.setString(1, "peter-pan");
- ps.setString(2, "peter@look.com");
4.执行操作
ps.execute();
5.关闭资源
- try {
- if (ps != null)
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
图示:
1.准备通用方法,加入形参:sql语句和可变形参的占位符;注:sql中占位符的个数与可变形参的长度相同
public void update(String sql,Object...args){}
2.在方法内部:
2.1.获取连接,返回PreparedStatement实例
- Connection conn = null;
- PreparedStatement ps = null;
-
- conn = JdbcUtils.getConnection();
- ps = conn.prepareStatement(sql);
2.2使用for循环填充占位符
- for (int i = 0; i < args.length; i++) {
- ps.setObject(i+1,args[i]);
- }
3.执行操作
4.关闭资源
方法体代码示例:
- public void update(String sql,Object...args){//sql中占位符的个数与可变形参的长度相同
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- conn = JdbcUtils.getConnection();
- ps = conn.prepareStatement(sql);
-
- for (int i = 0; i < args.length; i++) {
- ps.setObject(i+1,args[i]);
- }
- ps.execute();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- JdbcUtils.closeResource(conn,ps);
- }
操作代码示例:
- public void test3(){
- String sql = "update `order` set order_name = ? where order_id = ?";
- update(sql, "DD", "2");
-
- String sql1 ="update `order` set order_name =? where order_id=?";
- update(sql1,"CG","1");
- }
1.获取数据库连接,返回PreparedStatement实例
2.调用setObject()方法给占位符赋值
3.执行并返回结果集
rs = ps.executeQuery();
4.处理结果集、并关闭资源
- if (rs.next()) {//next():判断结果集的下一条是否有数据,如果有数据返回true,并指针下移,如果返回false,指针不会下移
- //获取当前这条数据的各个字段值
- int id = rs.getInt(1);
- String name = rs.getString(2);
- String email = rs.getString(3);
- Date birth = rs.getDate(4);
-
- //将数据封装为一个对象
- Customer customer = new Customer(id, name, email, birth);
- System.out.println(customer);
- }
图示:
1.编写Query()方法:传入形参:泛型,sql语句及可变形参()
public List getForList(Class clazz, String sql, Object... args ) {
2.获取数据库连接、返回PreparedStatement实例
3.使用for循环获取占位符并设置
4.执行
5.获取结果集的元数据
ResultSetMetaData rsmd = rs.getMetaData();
6.通过rsmd获取结果集的列数
int columnCount = rsmd.getColumnCount();
7.处理结果集
- if (rs.next()) {
- //创建一个泛型对象实例
- T t = clazz.newInstance();
- //处理结果集一行数据中的每一列
- for (int i = 0; i < columnCount; i++) {
- //获取数据
- Object columValue = rs.getObject(i + 1);
- //获取每个列的列名
- String columnLabel = rsmd.getColumnLabel(i + 1);
- //给t对象指定columName的属性,赋值为columValue:通过反射
- Field field = clazz.getDeclaredField(columnLabel);
- //field.setAccessible(true) 设置可以访问private变量的变量值
- field.setAccessible(true);
- //给对象的Filed属性设置value
- field.set(t, columValue);
- }
- //返回结果
- return t;
- }
8.关闭资源
方法体代码:
- public
T getInstance(Class clazz, String sql, Object... args) { - Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- conn = JdbcUtils.getConnection();
- ps = conn.prepareStatement(sql);
- for (int i = 0; i < args.length; i++) {
- ps.setObject(i + 1, args[i]);
- }
- rs = ps.executeQuery();
-
- //获取结果集的元数据:ResultSetMetaData
- ResultSetMetaData rsmd = rs.getMetaData();
- //通过rsmd获取结果集中的列数
- int columnCount = rsmd.getColumnCount();
- if (rs.next()) {
- //创建一个泛型对象实例
- T t = clazz.newInstance();
- //处理结果集一行数据中的每一列
- for (int i = 0; i < columnCount; i++) {
- //获取数据
- Object columValue = rs.getObject(i + 1);
- //获取每个列的列名
- String columnLabel = rsmd.getColumnLabel(i + 1);
- //给t对象指定columName的属性,赋值为columValue:通过反射
- Field field = clazz.getDeclaredField(columnLabel);
- //field.setAccessible(true) 设置可以访问private变量的变量值
- field.setAccessible(true);
- field.set(t, columValue);
- }
- return t;
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- JdbcUtils.closeResource(conn,ps,rs);
-
- }
-
- return null;
- }
测试代码:
- @Test
- public void test(){
- /* String sql = "select id,name,email from customers where id =?";
- Customer customer = getInstance(Customer.class,sql,12);
- System.out.println(customer);*/
-
- String sql1 = "select order_id orderId,order_name orderName from `order` where order_id = ?";
- Order order = getInstance(Order.class,sql1,1);
- System.out.println(order);
-
- String sql2 = "select id,name,email,birth from `customers` where id = ?";
- Customer cust = getInstance(Customer.class, sql2, 2);
- System.out.println(cust);
- }
【注意】:针对于表的字段名与类的属性名不相同的情况:
1.必须在声明sql时,使用类的属性名来命名字段的别名
2.使用ResultSetMetaData时,需要使用getColumnLabel()来替换getColumnName(),获取列的别名。
说明:如果sql中没有给字段起别名,getColumnLabel()获取的就是列名