JDBC中获取连接的几种方式,快来看看吧

注:下面的连接均是以mysql为例,@Test是Junit4的注解用于测试,Properties不会的话可自行百度

可能存在某些不规范的说法或者错误,恳请各位指出错误

 方式一:

import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;

@Test
public void test1() throws SQLException {
        //方式1:Driver是mysql包里面的
        Driver driver=new Driver();
        //指定url
        String url="jdbc:mysql://localhost:3306/jdbc";
        //将用户名和密码封装到Properties
        Properties properties=new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","1234");
        //传入url和properties
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

方式二:

    import com.mysql.cj.jdbc.Driver;
    import java.sql.Connection;

    @Test
    public void test2() throws Exception {
        //方式二
        //包名注意是com.mysql.cj.jdbc.Driver还是com.mysql.jdbc.Driver,jar包版本不同包名不同

        Class clas = Class.forName("com.mysql.cj.jdbc.Driver");
        //调用newInstance()方法创建一个Driver对象

        Driver driver = (Driver) clas.newInstance();
        //指定url,下面方法同方式一
        String url="jdbc:mysql://localhost:3306/jdbc";
        //将用户名和密码封装到Properties
        Properties properties=new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","1234");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

方式三:通过反射以及调用DriverManager的getConnection方法获取连接

    import java.sql.DriverManager;
    import java.sql.Connection;

    @Test
    public void test3() throws Exception {
        //方式三
        //通过反射
       Class.forName("com.mysql.cj.jdbc.Driver");
        //指定url,数据库的用户名和密码
       String url="jdbc:mysql://localhost:3306/jdbc";
       String user="root";
       String password="1234";
       //调用DriverManager的getConnection方法获取连接,在idea中按住ctrl+p可以看参数,把url,用户名,密码分别填上。
       Connection connection = DriverManager.getConnection(url, user, password);
       System.out.println(connection);
    }

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

方式四:最终版,读取properties文件

       首先,在类加载目录下创建一个properties文件,例如mysql.properties;maven工程的话可以在resource目录下建

user=root    //数据库的用户名
password=1234    //数据库的密码
url=jdbc:mysql://localhost:3306/jdbc  //数据库的url
driver=com.mysql.cj.jdbc.Driver  //驱动的位置注意是com.mysql.cj.jdbc.Driver还是com.mysql.jdbc.Driver要根据不同版本的jar包进行确定
    import java.sql.DriverManager;
    import java.sql.Connection;

    @Test
    public void test4() throws Exception {
        //1,读取配置文件的信息
        //通过反射获取类加载器,然后读取properties文件,下面的ConnectionFunction是当前类的类名
        InputStream resourceAsStream = ConnectionFunction.class.getClassLoader().getResourceAsStream("mysql.properties");
        Properties properties=new Properties();
        //2,加载配置文件
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        //3,加载驱动
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

执行后打印connection类似与下面这样,就代表数据库连接成功

com.mysql.cj.jdbc.ConnectionImpl@4f638935

考虑到以后使用jdbc时,都要获取连接,那么我们可以对上面的方法进行简单的封装,建一个类命名为JdbcUtils,封装获取连接方法的返回值是Connection,注意是sql包下的Connection,不要导错包,工具类的方法定义为static,然后把最终版的获取连接拷贝进来,最后返回这个连接就可以了。

package com.yajxweb.jdbc.utils;

import com.yajxweb.jdbc.ConnectionFunction;


import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {

    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
       //1,读取配置文件的信息
        //通过反射获取类加载器,然后读取properties文件,下面的ConnectionFunction是当前类的类名
        InputStream resourceAsStream = ConnectionFunction.class.getClassLoader().getResourceAsStream("mysql.properties");
        Properties properties=new Properties();
        //2,加载配置文件
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        //3,加载驱动
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }

   
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值