Java封装JDBC通用工具类

Java封装通用工具类

  • 一、JDBC配置类
  • 二、配置文件
  • 三、通用工具类

一、JDBC配置类

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class Config {
    private static String username;
    private static String password;
    private static String url;
    private static String driverClass;

    static{
        Properties prop = new Properties();
        try {
            //读取配置文件
            InputStream in = new FileInputStream("JavaDemo10/config/jdbc.properties");
            //加载配置文件内容
            prop.load(in);
            // acquire the properties from file
            username = prop.getProperty("username");
            password = prop.getProperty("password");
            url = prop.getProperty("url");
            driverClass = prop.getProperty("driverClass");
            // register the driver
            if(driverClass != null && url != null && username != null && password != null){
                //加载驱动
                Class.forName(driverClass);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * @description:  获取连接
     * @param
     * @return: java.sql.Connection
     */
    public static Connection getConnection() throws SQLException {
        Connection con = null;
        try{
            con = DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return con;
    }
    /**
     * @description:  释放资源
     * @param con
     * @param stat
     * @param res
     */

    public static void close(Connection con, Statement stat , ResultSet res) throws SQLException {
        if(con != null){
            con.close();
        }
        if (stat != null) {
            stat.close();
        }
        if (res != null) {
            res.close();
        }
    }
}

二、配置文件

#mysql8驱动
driverClass = com.mysql.cj.jdbc.Driver
#用户名
username = root
#数据库密码
password = 123456
#数据库url
url = jdbc:mysql://localhost:3306/databaseName

三、通用工具类

import java.sql.*;

/**
 * @Description: jdbc封装工具类
 * @Version: 1.0
 */
public class JdbcUtils {
    private static Connection con = null;
    private static Statement st = null;
    private static ResultSet rs = null;
    private static PreparedStatement ps = null;
    /**
     * @description: 通用更新封装
     * @param sql    sql语句
     * @param params   传入多参,也可以以数组形式传入
     * @return: java.sql.ResultSet
     */

    public static int update(String sql,Object ...params) throws SQLException {
        int flag = 0;
        try {
            con = Config.getConnection();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1,params[i]);
            }
            flag = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Config.close(con,ps,null);
        }
        return flag;
    }

    /**
     * @description:  通用查询
     * @param sql
     * @param params
     * @return: java.sql.ResultSet
     */
    public static ResultSet query(String sql,Object ...params) throws SQLException {
        try {
            con = Config.getConnection();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rs = ps.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;

    }
}

你可能感兴趣的:(JAVA,java,开发语言,jdbc,jdbc工具类)