Contents

JDBC

1.pom 依赖

1
2
3
4
5
6
7
    <dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>
   </dependencies>

2.连接测试

2.1 Test01.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.forsre.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

public class Test01 {
    public static void main(String[] args) {
            try {
                //1.提供java.sql.Driver接口实现类的对象
                Driver driver = null;
                driver = new com.mysql.jdbc.Driver();

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

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

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

2.2 Test02.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.forsre.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.util.Properties;

public class Test02 {
    public static void main(String[] args) {
        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://192.168.8.161:3306/hql";

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

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

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
说明:相较于方式一,这里使用反射实例化Driver,不在代码中体现第三方数据库的API。体现了面向接口编程思想。
*/

2.3 Test03.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.forsre.jdbc;

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

public class Test03 {
    public static void main(String[] args) {
        try {
            //1.数据库连接的4个基本要素:
            String url = "jdbc:mysql://192.168.8.161:3306/hql";
            String user = "root";
            String password = "000000";
            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();
        }
    }
}

/*
说明:使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。
*/

2.4 Test04java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.forsre.jdbc;

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

public class Test04 {
    public static void main(String[] args) {
        try {
            //1.数据库连接的4个基本要素:
            String url = "jdbc:mysql://192.168.8.161:3306/hql";
            String user = "root";
            String password = "000000";
            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());
                } 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();
        }
        
    }
}

说明:不必显式的注册驱动了。因为在DriverManager的源码中已经存在静态代码块,实现了驱动的注册。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    //
    // Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

2.5 Test05.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.forsre.jdbc;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class Test05 {
    public static void main(String[] args) throws Exception {
        //1.加载配置文件
        InputStream is = Test05.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);

    }
}


/*
说明:使用配置文件的方式保存配置信息,在代码中加载配置文件

**使用配置文件的好处:**

①实现了代码和数据的分离,如果需要修改配置信息,直接在配置文件中修改,不需要深入代码
②如果修改了配置信息,省去重新编译的过程。
*/
1
2
3
4
5
6
7
#jdbc.properties
#放置至src/main/resources目录内

user=root
password=000000
url=jdbc:mysql://192.168.8.161:3306/hql
driverClass=com.mysql.jdbc.Driver