创建连接的5种方式

/**
 * 连接jdbc的5种方式
 */

public class jdbc02 {
    // 方式1:
    @SuppressWarnings({"all"})
    @Test
    public void test1() throws Exception{
        // 1.获得驱动
        Driver driver = new Driver();
        // 2.获得连接
        String url = "jdbc:mysql://localhost:3306/hspdb02";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","zw20010108");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
        connect.close();
    }

    // 方式2:
    @SuppressWarnings({"all"})
    public void test2() throws Exception{
        // 1.获得驱动
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        // 2.获得连接
        String url = "jdbc:mysql://localhost:3306/hspdb02";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","zw20010108");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
        connect.close();
    }

    // 方式3:
    @Test
    public void test3() throws Exception{
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/hspdb02";
        String user = "root";
        String password = "zw20010108";
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        connection.close();
    }

    // 方式4:
    @SuppressWarnings({"all"})
    @Test
    public void test4() throws Exception{
        /*
        static {
            try {
                DriverManager.registerDriver(new Driver());
            } catch (SQLException var1) {
                throw new RuntimeException("Can't register driver!");
            }
        }
         */
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/hspdb02";
        String user = "root";
        String password = "zw20010108";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        connection.close();
    }

    // 方式5
    @Test
    public void test5() throws Exception{
        Properties properties = new Properties();
        properties.load(new FileReader("src\\mysql.properties"));
        Class.forName(properties.getProperty("Driver"));
        Connection connection = DriverManager.getConnection(
                properties.getProperty("url"),
                properties.getProperty("user"),
                properties.getProperty("pwd"));
        System.out.println(connection);
        connection.close();
    }
}

在方式5中的配置文件如下:

 mysql.properties:

Driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/hspdb02
user=root
pwd=*****

 

posted @ 2023-01-29 09:30  zwGitOne  阅读(72)  评论(0)    收藏  举报