首页 > 技术文章 > JDBC获取连接(Mysql举例)

linglongfang 2020-04-12 18:48 原文

获取数据库连接的4大基本要素

1. 由数据库厂商实现的的java.sql.Driver接口的实现类:例如mysql是:com.mysql.jdbc.Driver

2.数据库地址:例如:jdbc:mysql://localhost:3306/test

3.用户名:例如:root

4.密码:例如:123456

通常,我们把Driver的实现类叫做driverClass,数据库地址叫做url,将用户名叫做user,将密码叫做password

方式一(不推荐):直接通过创建Mysql实现的java.sql.Driver的实现类来完成。

@Test
public void testConnection() {
    try {
        //1.提供java.sql.Driver接口实现类的对象
        Driver driver = null;
        driver = new com.mysql.jdbc.Driver();

        //2.提供url,指明具体操作的数据
        String url = "jdbc:mysql://localhost:3306/test";

        //3.提供Properties的对象,指明用户名和密码
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "abc123");

        //4.调用driver的connect(),获取连接
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

方式二(不推荐):通过反射出Mysql实现的Driver类来获取连接。

@Test
public void testConnection() {
    try {
        //1.实例化Driver
        String className = "com.mysql.jdbc.Driver";
        Class clazz = Class.forName(className);
        Driver driver = (Driver) clazz.newInstance();

        //2.提供url,指明具体操作的数据
        String url = "jdbc:mysql://localhost:3306/test";

        //3.提供Properties的对象,指明用户名和密码
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "abc123");

        //4.调用driver的connect(),获取连接
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

方式三:通过java.sql.Manager去实现数据库连接,需要将Driver在DriverManager中注册。

@Test
public void testConnection() {
    try {
        //1.数据库连接的4个基本要素:
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "abc123";
        String driverName = "com.mysql.jdbc.Driver";

        //2.实例化Driver
        Class clazz = Class.forName(driverName);
        Driver driver = (Driver) clazz.newInstance();
//3.注册驱动 DriverManager.registerDriver(driver);
//4.获取连接 Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); } catch (Exception e) { e.printStackTrace(); } }

方式四:Mysql中Driver的实现类中,静态代码块中实例化了Driver,并将其在DriverManager中注册了。

 @Test
 public void testConnection() {
    try {
        //1.数据库连接的4个基本要素:
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "abc123";
        String driverName = "com.mysql.jdbc.Driver";

//2.加载驱动 (①实例化Driver ②注册驱动) Class.forName(driverName);
//Driver driver = (Driver) clazz.newInstance(); //3.注册驱动 //DriverManager.registerDriver(driver); /* 可以注释掉上述代码的原因,是因为在mysql的Driver类中声明有: static { try { DriverManager.registerDriver(new Driver()); //默认实例化了Driver,并在DriverManager中注册实例化的Driver } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } } */ //3.获取连接 Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); } catch (Exception e) { e.printStackTrace(); } }

方式五(推荐):使用配置文件取获取数据库连接,使得代码和具体数据库实现了解耦。(配置文件jdbc.properties应该放在src目录下)

@Test
public  void testConnection() throws Exception {
    //1.加载配置文件
    InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
    Properties pros = new Properties();
    pros.load(is);
        
    //2.读取配置信息
    String user = pros.getProperty("user");
    String password = pros.getProperty("password");
    String url = pros.getProperty("url");
    String driverClass = pros.getProperty("driverClass");

    //3.加载驱动
    Class.forName(driverClass);

    //4.获取连接
    Connection conn = DriverManager.getConnection(url,user,password);
    System.out.println(conn); 
}

 

推荐阅读