JDBC第二天---JDBC工具类

  JDBC工具类的作用就是方便使用数据操作方法,设计本着方便维护,可重复使用的思想。

public class JDBCUtil2 {
    private static final String URL="jdbc:mysql://localhost/表名?useUnicode=true&useSSL=false&character=utf8";
    private static final String DRIVER="com.mysql.jdbc.Driver";
    private static final String USE="root";
    private static final String PASSWORD="root";

    /*
    * 加载驱动,静态代码,只加载一次
    * */
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*
    * 创建连接
    * */
    public static Connection getConn(){
        Connection conn=null;
        try {
            conn = DriverManager.getConnection(URL, USE, PASSWORD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    /*
    * 此方法给sql占位符赋值  
    * Object... args :可变参数列表。可以输入不定个参数
    * */
    public static void setParameter(PreparedStatement ps,Object... args){
        try{
            if(args.length>0){
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
    * 执行DQL,使用List<List>接收
    * */
    public static List<List> queryList(String sql,Object... args){
//        获取连接
        Connection conn = getConn();
        PreparedStatement ps=null;
        ArrayList<List> bigList=null;
        ResultSet rs=null;
//        3、预编译sql
        try {
            ps = conn.prepareStatement(sql);
            setParameter(ps,args);

//          4、执行sql
            rs = ps.executeQuery();

//            使用双层List<List>接收结果
            bigList = new ArrayList<>();
            while (rs.next()){
                ArrayList<Object> sList = new ArrayList<>();
                for (int i=1;i<=rs.getMetaData().getColumnCount();i++){
                    sList.add(rs.getObject(i));
                }
                bigList.add(sList);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(rs,ps,conn);
        }
        return bigList;
    }

    /*
    * 执行DQL,使用List<Map>来接收结果
    * */
    public static List<Map> queryMapList(String sql,Object... args){
        //        获取连接
        Connection conn = getConn();
        PreparedStatement ps=null;
        ArrayList<Map> bigList=null;
        ResultSet rs=null;
//        3、预编译sql
        try {
            ps = conn.prepareStatement(sql);
            setParameter(ps,args);

//          4、执行sql
            rs = ps.executeQuery();

//            使用双层List<List>接收结果
            bigList = new ArrayList<>();
            while (rs.next()){
                HashMap<String, Object> hm = new HashMap<>();
                for (int i=1;i<=rs.getMetaData().getColumnCount();i++){
                    hm.put(rs.getMetaData().getColumnName(i),rs.getObject(i));
                }
                bigList.add(hm);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(rs,ps,conn);
        }
        return bigList;
    }

    /*
    * 执行DML
    * */
    public static int updataSQL(String sql,Object... args){
        //        获取连接
        Connection conn = getConn();
        PreparedStatement ps=null;
//        3、预编译sql
        try {
            ps = conn.prepareStatement(sql);
            setParameter(ps,args);

//          4、执行sql
            int i1 = ps.executeUpdate();
            return i1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(null,ps,conn);
        }
        return -1;
    }

    /*
     * 此方法可以获取主键id。就是在插入数据时,返回新增的id
     * 场景使用:主要就是用在新用户注册时,返回新增用户的id,便于操作用户
     * */
    public static int getPrimaryK(String sql,Object... args){
        //        获取连接
        Connection conn = getConn();
        PreparedStatement ps=null;
        ResultSet st=null;
//        3、预编译sql
        try {
            ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            setParameter(ps,args);

//          4、执行sql
            int i1 = ps.executeUpdate();
            st = ps.getGeneratedKeys();
            //            因为新增注册用户是一次一个,所以就是使用if,不用while
            if(st.next()){
                int anInt = st.getInt(1);
                return anInt;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(st,ps,conn);
        }
        return -1;
    }

    /*
    * 此方法用来关闭链接,驱动,结果集
    * */
    public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            if(ps!=null){
                ps.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            if(conn!=null){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/fbbg/p/14205290.html