自己封装数据库工具类

时间:2022-07-28
本文章向大家介绍自己封装数据库工具类,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.代码

配置文件

跨平台方案: 创建properties配置文件 创建Properties集合: • public static final Properties prop = new Properties(); 静态代码块中,使用输入流,读取配置文件。

package d04_dbutils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * zt
 * 2020/8/20
 * 15:53
 * 封装数据库工具类
 * 1.注册驱动
 * 2.获取连接
 * 3.关闭资源
 * ------------------------------
 * 4.执行命令(增、删、改)
 * 5.查询
 * 6.事务操作
 */
public class DbUtils {
    //驱动名称
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    //1.注册驱动
    static {
        try {
            //读取配置文件
            Properties properties = new Properties();
            //类加载器加载配置文件
            InputStream is = DbUtils.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(is);
            is.close();
            //赋值
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            user=properties.getProperty("user");
            password=properties.getProperty("password");

            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("注册驱动失败");
        }
    }
    //2.获取连接
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //3.关闭连接
    public static void closeAll(ResultSet rs, Statement stat,Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
            if(stat!=null){
                stat.close();
            }
            if(conn!=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //4.执行增、删、改 preparedStatement
    public static int executeUpdate(String sql,Object...params){
        Connection conn = null;
        PreparedStatement pstat = null;
        try {
            //给参数赋值
            conn = getConnection();
            pstat = conn.prepareStatement(sql);
            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    pstat.setObject(i+1, params[i]);
                }
            }
            return pstat.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeAll(null, pstat, conn);
        }
        return -1;
    }
}
package d04_dbutils;

/**
 * zt
 * 2020/8/20
 * 16:00
 * 工具类测试(查询)
 */
public class JdbcDemo6 {
    public static void main(String[] args) throws Exception {
       /* Connection conn = DbUtils.getConnection();
        PreparedStatement pstat = conn.prepareStatement("select * from user");
        ResultSet rs = pstat.executeQuery();
        while(rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String password = rs.getString("password");
        }
        DbUtils.closeAll(rs, pstat, conn);*/

       String sql = "insert into user(name,password) values(?,?)";
        for (int i = 0; i < 10; i++) {
            Object[] params = {"大明"+i,"123456"};
            DbUtils.executeUpdate(sql, params);
        }

    }
}

2.运行结果