【Java】JDBC连接MySQL驱动
獲取驅(qū)動(dòng)jar包
官網(wǎng)下載jar包導(dǎo)入工程
安裝Java和安裝MySQL這里就不說了,可以通過官網(wǎng)鏈接下載驅(qū)動(dòng)。
注意如果是WindowsOS不要直接下Windows,那個(gè)不是JDBC,應(yīng)該選上面的"Selecting Operating System…“,再選"Plantform Independent”,在這里選那個(gè).zip文件。Unix/Linux的選擇上面的.tar.gz文件。
想選擇版本的話,選擇Archives:
打開.zip就有.jar,自取并導(dǎo)入工程。
jar包導(dǎo)入方法:
- 把.jar導(dǎo)入idea的話,推薦閱讀這篇文章。
- 把.jar導(dǎo)入eclipse的話,推薦閱讀這篇文章。
Maven引入jar包
訪問Maven中央倉庫,搜索mysql-connector-java,在頁面中尋找對應(yīng)版本的pom引入配置。
例如,8.0.28版本的引入配置是:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version> </dependency>隨后引入jar包即可。
解讀URL
URL為jdbc:mysql://localhost:3306/[database_name]:
- jdbc:mysql://:JDBC連接方式
- localhost:本機(jī)地址
- 3306:SQL數(shù)據(jù)庫的端口號(MySQL用這個(gè)就行了)
- [database_name]:待連接的數(shù)據(jù)庫的名稱
一般來說,只需要自己寫一下DatabaseName即可,注意這個(gè)庫應(yīng)該是存在的,否則會(huì)報(bào)錯(cuò)。
版本問題
筆者使用的jar包是 mysql-connector-java-8.0.19.jar ,mysql-connector-java版本在8以上時(shí)應(yīng)該使用:com.mysql.cj.jdbc.Driver。
亂碼問題
運(yùn)行起來可能還會(huì)有亂碼問題,我遇到過的一個(gè)亂碼問題的異常棧是這樣的:
java.sql.SQLException: The server time zone value ‘�й���??��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.
????at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
????at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
????at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
????at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
????at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
????at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
????at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
????at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456)
????at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
????at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
????at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
????at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
????at main.JDBCTest.main(JDBCTest.java:19)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value ‘�й���??��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.
??at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
??at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
??at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
??at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
??at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
??at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
??at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
??at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2118)
??at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2142)
??at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1310)
??at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967)
??at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
??… 6 more
處理方法是:
將URL改為jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
Demo
數(shù)據(jù)庫名、用戶名、密碼 這三個(gè)需要自己替換,注意版本是否與筆者大概一致,如果是8-版本就很不一樣!
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;public class JDBCDemo {public static void main(String[] args) {String driverName="com.mysql.cj.jdbc.Driver";String dbURL="jdbc:mysql://localhost:3306/dbName?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";// 你的登錄名,自己寫,比如rootString userName="userName";// 你的登錄密碼,自己寫String userPassword="userPassword";try {Class.forName(driverName);System.out.println("加載MySQL驅(qū)動(dòng)成功");} catch (ClassNotFoundException e) {System.out.println("加載MySQL驅(qū)動(dòng)失敗");}try (Connection dbConnection = DriverManager.getConnection(dbURL, userName, userPassword)) {System.out.println("連接數(shù)據(jù)庫成功");} catch(SQLException e) {System.out.println("數(shù)據(jù)庫連接失敗");}}}運(yùn)行結(jié)果:
加載MySQL驅(qū)動(dòng)成功 連接數(shù)據(jù)庫成功總結(jié)
以上是生活随笔為你收集整理的【Java】JDBC连接MySQL驱动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【高等数学】一元函数积分表
- 下一篇: 【MySQL】基于MySQL的SQL核心