使用jdbc 的properties配置文件来连接数据库

本文介绍了如何在Java项目中创建db.properties配置文件,详细配置了MySQL连接参数,并展示了如何使用Properties对象动态加载和获取这些配置。关键步骤包括创建配置文件、静态代码块中加载数据库连接信息,以及getConnection方法的应用。
摘要由CSDN通过智能技术生成

一、创建配置文件

在项目跟目录下,创建文件,输入“db.properties”文件名。

文件中的内容:(我用的是mysql数据库)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/abc
username=root
password=123456
<!-- 初始化连接 -->
initialSize=10
<!--最大连接数量  -->
maxActive=50
<!-- 最大空闲连接 -->
maxIdle=20
<!-- 最小空闲连接 -->
minIdle=5
<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000

二、加载配置文件:Properties对象

       对应properties文件处理,开发中也使用Properties对象进行。我们将采用加载properties文件获得流,然后使用Properties对象进行处理。

JDBCUtils.java中编写代码

public class JDBCUtils {
 

    private static String driver;

    private static String url;

    private static String user;

    private static String password;

    // 静态代码块

    static {
        try {
            // 1 使用Properties处理流

            // 使用load()方法加载指定的流

            Properties props = new Properties();

            Reader is = new FileReader("db.properties");

            props.load(is);

            // 2 使用getProperty(key),通过key获得需要的值,

            driver = props.getProperty("driver");

            url = props.getProperty("url");

            user = props.getProperty("user");

            password = props.getProperty("password");

        } catch (Exception e) {
            throw new RuntimeException(e);

        }

    }

 

    /**

     * 获得连接

     */

    public static Connection getConnection() {
        try {
            // 1 注册驱动

            Class.forName(driver);

            // 2 获得连接

            Connection conn = DriverManager.getConnection(url, user, password);

            return conn;

        } catch (Exception e) {
            throw new RuntimeException(e);

        }

    }

}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在MyEclipse中添加MySQLJDBC驱动程序。可以通过以下步骤完成: 1. 下载MySQLJDBC驱动程序(JAR文件)。 2. 在MyEclipse中,右键单击项目文件夹并选择“Properties”。 3. 在“Properties”对话框中,选择“Java Build Path”。 4. 在“Java Build Path”下,选择“Libraries”选项卡。 5. 点击“Add External JARs”按钮,并选择下载的MySQL JDBC驱动程序文件。 6. 点击“OK”按钮保存更改。 完成添加MySQL JDBC驱动程序之后,你可以按照以下步骤在MyEclipse中使用JDBC连接MySQL数据库: 1. 在MyEclipse中,打开你的Java项目。 2. 在Java文件中,添加以下导入语句: ``` import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; ``` 3. 连接MySQL数据库: ``` Connection conn = null; String url = "jdbc:mysql://localhost:3306/yourdatabase"; String user = "yourusername"; String password = "yourpassword"; try { conn = DriverManager.getConnection(url, user, password); System.out.println("Connection successful!"); } catch (SQLException e) { System.out.println("Connection failed!"); e.printStackTrace(); } ``` 注意将“yourdatabase”替换为你要连接的数据库名称,“yourusername”和“yourpassword”替换为你的MySQL用户名和密码。 4. 使用完毕后,关闭连接: ``` try { conn.close(); System.out.println("Connection closed."); } catch (SQLException e) { e.printStackTrace(); } ``` 以上就是使用MyEclipse连接MySQL数据库的基本步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值