JDBC之通用的CRUD

增删改

package com.utils;

import com.domain.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author 承夕
 * @date 2020/2/29 0029 - 22:23
 * @contact:https://github.com/chengxi0
 */
public class CreateUpdateDelete {

    public static void main(String[] args) {
        String sql = "insert into user (name ,password ,address ,phone) values (?,?,?,?)" ;
        User user = new User();
        user.setU_name("赵六");
        user.setU_password("123abc");
        user.setU_address("sh");
        user.setU_phone("1234523242");
        int i = create(sql, user.getU_name(), user.getU_password(), user.getU_address(), user.getU_phone());
        System.out.println(i);

    }


    public static int create(String sql, Object... args) {
        Connection conn = null ;
        PreparedStatement pstm = null ;

        try {
            //获取连接
            conn = JDBCUtils.getConnection();

            //获取PreparedStatement
            pstm = conn.prepareStatement(sql);

            //填充占位符
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i + 1, args[i]);
            }

            int i = pstm.executeUpdate();

            return i ;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  0 ;
    }
}

查询

package com.utils;

import com.domain.User;

import java.io.File;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

/**
 * @author 承夕
 * @date 2020/2/29 0029 - 11:14
 * @contact:https://github.com/chengxi0
 */
public class Read {

    public static void main(String[] args) {
        String sql = "select id, name u_name, password u_password , address u_address, phone u_phone from user where " +
                "id = ?";
        ArrayList<User> users = read(User.class, sql,6);
        for (User u : users) {
            System.out.println(u);
        }
    }

    public static <T> ArrayList<T> read(Class<T> clazz , String sql, Object... args) {

        Connection conn = null ;
        PreparedStatement pstm = null ;
        ResultSet rs = null ;

        try {

            //获取数据库连接
            conn = JDBCUtils.getConnection();

            //获取PreparedStatement对象
            pstm = conn.prepareStatement(sql);

            //填充占位符
            for (int i = 0; i <args.length; i++) {
                pstm.setObject(i +1, args[i]);
            }

            //获取结果集对象
            rs = pstm.executeQuery();
            //获取元数据集
            ResultSetMetaData rsmd = rs.getMetaData();
            //获取多少列
            int columnCount = rsmd.getColumnCount();

            ArrayList<T> ts = new ArrayList<>();
            while (rs.next()) {
                //创建对象,赋值
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取每一行中每一个字段的值
                    Object object = rs.getObject(i + 1);
                    //获取查询表时的字段的别名
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, object);
                }
                ts.add(t);
            }
            return ts ;

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null ;
    }

}

注意点:

在查询操作里面,我们通过利用查询语句中的别名和类中的属性一致,使其能够利用反射找到对应类中的域。因此更加常用的是获取该列的别名而不是获取改列的原名

在执行查询或者增删改操作之前记得要填充占位符?

方法的返回值需要声明泛型,方法的参数使用可变参数,其参数的个数与占位符的个数保持一致原则

而增删改的操作比较简单

发布了55 篇原创文章 · 获赞 4 · 访问量 1056

猜你喜欢

转载自blog.csdn.net/weixin_45062761/article/details/104561691