使用配置文件写JDBC

1.jdbc.properties

msg1=com.mysql.cj.jdbc.Driver    //JDBC 驱动类的全名
msg2=jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT
msg3=root    //数据库用户名
msg4=123456    //数据库密码

2.测试类

public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
        //创建一个 Properties 对象,用于存储配置信息。
        Properties properties = new Properties();

        //使用 ClassLoader 获取名为 jdbc.properties 的资源文件的输入流。这个文件应位于项目的类路径下。
        InputStream inputStream = Test01.class.getClassLoader().getResourceAsStream("jdbc.properties");

        //调用 load 方法将文件中的键值对加载到 properties 对象中。
        properties.load(inputStream);

        String driver = properties.getProperty("msg1");
        String url = properties.getProperty("msg2");
        String name = properties.getProperty("msg3");
        String pwd = properties.getProperty("msg4");
        
        //动态加载指定的 JDBC 驱动。这样可以在运行时注册该驱动。
        Class.forName(driver);
        
        //DriverManager.getConnection(url, name, pwd):根据提供的 URL、用户名和密码获取数据库连接。   
        Connection root = DriverManager.getConnection(url, name, pwd);
        System.out.println(root);

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用jdbc连接数据库时,可以使用druid作为连接池来管理连接,具体配置如下: 1. 导入druid的相关jar包,如下: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.4</version> </dependency> ``` 2. 在配置文件中添加druid的相关配置: ``` jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false jdbc.username=root jdbc.password=123456 # druid配置 # 初始化连接数 druid.initialSize=5 # 最小空闲连接数 druid.minIdle=5 # 最大活跃连接数 druid.maxActive=20 # 获取连接等待超时的时间(单位:毫秒) druid.maxWait=60000 # 配置检测的sql语句,用于检测连接是否有效 druid.validationQuery=SELECT 1 # 检测连接是否有效的超时时间(单位:毫秒) druid.validationQueryTimeout=3000 # 是否开启自动提交事务 druid.defaultAutoCommit=true # 是否开启连接池监控功能 druid.stat.enable=true # 配置连接池监控的过滤器 druid.filter.stat.logSlowSql=true druid.filter.stat.slowSqlMillis=1000 ``` 3. 在代码中使用druid连接池,如下: ``` import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.SQLException; public class DruidUtil { private static DruidDataSource dataSource; static { try { // 加载配置文件 Properties props = new Properties(); InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); props.load(is); // 创建连接池 dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(props); } catch (Exception e) { e.printStackTrace(); } } /** * 获取连接 * * @return * @throws SQLException */ public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } /** * 关闭连接 * * @param conn * @param stmt * @param rs */ public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 使用上述代码获取连接时,会自动使用druid连接池来管理连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值