JDBC自我复习之获取数据库的连接

方式一:按部就班

@Test
    public void test1() throws SQLException {
        //1.实现Driver接口
        Driver driver = new com.mysql.jdbc.Driver();

        //2.选择数据库
        String url ="jdbc:mysql://localhost:3306/test";
        //url:数据库的地址
        //jdbc:主协议
        //mysql:子协议
        //localhost:ip地址
        //3306:默认mysql端口号
        //test:数据库名

        //3.将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        //4.获取连接
        Connection connect = driver.connect(url, info);

        System.out.println(connect);//com.mysql.jdbc.JDBC4Connection@63e31ee
    }

方式二:对方式一的迭代,使得不出现api,使得具有更好的可移植性

@Test
    public void test2() throws Exception{
        //1.获取Driver接口的实现对象
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2.提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/test";

        //3.提供用户名和密码
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");

        //4.获取连接
        Connection connect = driver.connect(url, properties);

        System.out.println(connect);
    }

方式三:使用DriverManager替换Driver

DriverManger:在java.sql这个包里面,管理一组 JDBC 驱动程序的基本服务

@Test
    public void test3() throws Exception{
        //1.获取Driver的实现对象
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        //2.提供连接的三个基本信息
        String url ="jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

        //3.注册驱动
        DriverManager.registerDriver(driver);

        //4.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }

方式四:可以只加载驱动,不用显示的注册驱动

@Test
    public void test4() throws Exception{
        //1.提供连接的三个基本信息
        String url ="jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

        //2.获取Driver的实现对象
        Class.forName("com.mysql.jdbc.Driver");

        //相较于方式三,可以省略如下的操作
        //因为mysql的Driver实现类中存在静态方法,可以注册驱动
//        Driver driver = (Driver) clazz.newInstance();
//        //3.注册驱动
//        DriverManager.registerDriver(driver);

        //4.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);

    }

方式五:将数据库连接需要的四个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接

/*  该方法的好处:
        1.实现了数据和代码的分离(实现了解耦)
        2.如果需要修改配置文件的信息,可以避免程序重新打包
     */
    @Test
    public void test5() throws Exception{

        //1.读取配置文件中的4个基本信息
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //2.加载驱动
        Class.forName(driver);

        //3.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);

    }

总结:方式五最好用且最常用!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JDBC(Java Database Connectivity)是Java平台用于与关系型数据库交互的一组API。要使用JDBC获取数据库连接,你需要遵循以下几个步骤: 1. **添加JDBC驱动依赖**:首先,确保你的项目中包含了对应数据库JDBC驱动。例如,如果你使用MySQL,你需要添加mysql-connector-java.jar到项目classpath。 2. **加载Driver**:在Java代码中,使用Class.forName()方法动态加载数据库驱动,如下所示: ```java Class.forName("com.mysql.cj.jdbc.Driver"); ``` 3. **创建Connection对象**:使用DriverManager.getConnection()或ConnectionPool提供的连接,提供数据库URL、用户名和密码: ```java String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "username"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); ``` 或者使用连接池(如HikariCP、Apache Commons DBCP等): ```java ConnectionPool pool = HikariDataSourceFactory.createPool(); Connection conn = pool.getConnection(); ``` 4. **处理连接**:在使用完毕后记得关闭连接,释放资源: ```java try { // 执行SQL操作 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM table"); ResultSet rs = pstmt.executeQuery(); // 处理结果集 } finally { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); // 或者放到finally块中,确保关闭 } } ``` 5. **异常处理**:JDBC操作可能会抛出SQLException,所以要进行适当的异常捕获和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值