java之mysql数据库连接

本文介绍了一个使用Druid作为数据库连接池的配置案例,详细展示了配置文件druid.properties的内容,并提供了一段Java代码示例,用于创建Druid连接池并获取数据库连接。

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

首先是数据库的配置文件,现在把mysql的配置文件放在.properties文件中

这是配置文件的信息名称是:druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.1.60:3306/edudb_app?characterEncoding=utf8
username=root
password=123456
filters=stat
initialSize=10
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200


接下来就是获取数据库的连接了

public class DruidPoolConnection {
    private static DruidPoolConnection databasePool = null;
    private static DruidDataSource dds = null;
    static {
        Properties properties = loadPropertyFile("druid.properties");
        try {
            dds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private DruidPoolConnection() {}
    public static synchronized DruidPoolConnection getInstance() {
        if (null == databasePool) {
            databasePool = new DruidPoolConnection();
        }
        return databasePool;
    }
    public DruidPooledConnection getConnection() throws SQLException {
        return dds.getConnection();
    }
    public static Properties loadPropertyFile(String fullFile) {
        String filePath = DruidPoolConnection.class.getClassLoader().getResource(fullFile).getPath();
        if (null == fullFile || fullFile.equals(""))
            throw new IllegalArgumentException(
                    "Properties file path can not be null : " + fullFile);
//        webRootPath = DruidPoolConnection.class.getClassLoader().getResource(fullFile)
//                .getPath();
//        webRootPath = new File(webRootPath).getParent();
        InputStream inputStream = null;
        Properties p = null;
        try {
            
            inputStream = new FileInputStream(filePath);
            p = new Properties();
            p.load(inputStream);
        } catch (FileNotFoundException e) {
            
            throw new IllegalArgumentException("Properties file not found: "
                    + filePath);
        } catch (IOException e) {
            throw new IllegalArgumentException(
                    "Properties file can not be loading: " + filePath);
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return p;
    }
}


标红的部分室外街可以调用的方法:

外界获得connection的方法是 DruidPoolConnection . getInstance ( ) . getConnection( )



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值