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;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值