JDC连接mysql数据库工具类

本文介绍了一个用于Java项目的数据库连接工具类,该类通过ResourceBundle读取配置文件中的数据库参数,实现数据库连接、预处理语句及结果集的获取,并提供了资源关闭的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.mysql.utils;

import java.sql.*;
import java.util.ResourceBundle;

public class DBUtil {
private static String driverClass ;
private static String jdbcUrl;
private static String user;
private static String password ;

static{

    ResourceBundle resource = ResourceBundle.getBundle("dbconfig");
    driverClass = resource.getString("driverClass");
    jdbcUrl= resource.getString("jdbcUrl");
    user = resource.getString("user");
    password = resource.getString("password");



}

public static Connection getConnection() throws Exception {
     Class.forName(driverClass) ;
   Connection conn = (Connection) DriverManager.getConnection(jdbcUrl, user, password);
    return conn;

}

public static PreparedStatement getPreparedStatement(String sql) throws Exception {
    PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
    return preparedStatement;

}
public static ResultSet getResultSet(PreparedStatement preparedStatement) throws SQLException {
    return preparedStatement.executeQuery();
}


public static void closeResource(ResultSet rs, PreparedStatement ps ) throws SQLException {
    if (rs!=null){
    rs.close();
    }
    if (ps !=null) {
        ps.close();
    }

}
public static void closeConnection() throws Exception {

    getConnection().close();

}

}

注:其他数据库只需要将配置文件修改即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值