JDBC01-----JDBC的基本使用

一. 什么是JDBC

1. JDBC(Java DataBase Connectivity, java数据库连接)就是用我们的java程序去连接数据库,它是一个执行SQL语句的JAVA API。(不管是什么数据库,只要提供了JDBC驱动就行)。

  note: 上面的jar包就是连接驱动包。

2. JDBC就是JAVA连接数据库的中间桥梁

3. JDBC的API

二. JDBC连接MySql

1. 新建立一个maven项目jdbctest

2. 在pom.xml中添加jdbc驱动依赖

1 <dependency>
2             <groupId>mysql</groupId>
3             <artifactId>mysql-connector-java</artifactId>
4             <version>6.0.6</version>
5         </dependency>
View Code

3. 在com.test.jdbctest包下新建立一个class: ConnectionClass

public class ConnectionClass {
    public static void main(String args[]) throws ClassNotFoundException, SQLException {
        //1.加载驱动(静态代码块中会创建一个驱动,来帮助我们连接JAVA到驱动之间的关联,但是驱动到数据库的连接还没有连上)
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接对象,URL是数据库地址
        String url="jdbc:mysql://localhost:3306/test";
        String user="Hermioner";
        String password="1234";
        Connection connection=DriverManager.getConnection(url, user, password);
        System.out.println(connection);//测试连接是否成功
    }

}
com.mysql.cj.jdbc.ConnectionImpl@17d99928

说明:

(1)加载驱动: 把com.mysql.jdbc.Driver这份字节码加载进JVM,当一份字节码被加载到JVM时,就会自动执行该字节码中的静态代码块。

(2)Driver的源码

 1 package com.mysql.jdbc;
 2 
 3 import java.sql.SQLException;
 4 
 5 /**
 6  * Backwards compatibility to support apps that call <code>Class.forName("com.mysql.jdbc.Driver");</code>.
 7  */
 8 public class Driver extends com.mysql.cj.jdbc.Driver {
 9     public Driver() throws SQLException {
10         super();
11     }
12 
13     static {
14         System.err.println("Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. "
15                 + "The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.");
16     }
17 }
View Code
 1 /*
 2   Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
 3 
 4   The MySQL Connector/J is licensed under the terms of the GPLv2
 5   <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
 6   There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
 7   this software, see the FOSS License Exception
 8   <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
 9 
10   This program is free software; you can redistribute it and/or modify it under the terms
11   of the GNU General Public License as published by the Free Software Foundation; version 2
12   of the License.
13 
14   This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15   without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   See the GNU General Public License for more details.
17 
18   You should have received a copy of the GNU General Public License along with this
19   program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
20   Floor, Boston, MA 02110-1301  USA
21 
22  */
23 
24 package com.mysql.cj.jdbc;
25 
26 import java.sql.SQLException;
27 
28 /**
29  * The Java SQL framework allows for multiple database drivers. Each driver should supply a class that implements the Driver interface
30  * 
31  * <p>
32  * The DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to
33  * connect to the target URL.
34  * 
35  * <p>
36  * It is strongly recommended that each Driver class should be small and standalone so that the Driver class can be loaded and queried without bringing in vast
37  * quantities of supporting code.
38  * 
39  * <p>
40  * When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a
41  * driver by doing Class.forName("foo.bah.Driver")
42  */
43 public class Driver extends NonRegisteringDriver implements java.sql.Driver {
44     //
45     // Register ourselves with the DriverManager
46     //
47     static {
48         try {
49             java.sql.DriverManager.registerDriver(new Driver());
50         } catch (SQLException E) {
51             throw new RuntimeException("Can't register driver!");
52         }
53     }
54 
55     /**
56      * Construct a new driver and register it with DriverManager
57      * 
58      * @throws SQLException
59      *             if a database error occurs.
60      */
61     public Driver() throws SQLException {
62         // Required for Class.forName().newInstance()
63     }
64 }
View Code

当类加载时,会执行如下静态代码块:

 static {
        try {
            java.sql.DriverManager.registerDriver(new Driver()); //在这个静态代码块里面会创建一个驱动,来帮助我们连接JAVA到驱动之间的关联,但是驱动到数据库的连接还没有连上
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

 (3)测试连接

        除了上面的打印对象,也可以通过mysql中的命令来查看是否连接成功:show processlist;

 参考文献:

高新强老师的视频课程:https://ke.qq.com/course/339214

猜你喜欢

转载自www.cnblogs.com/Hermioner/p/10231601.html
今日推荐