1. 程式人生 > >有關於用eclipse連線mysql資料庫出現的問題以及解決辦法

有關於用eclipse連線mysql資料庫出現的問題以及解決辦法

寫帖子是為了讓更多的程式設計師減少再除錯bug中的時間,也希望大家能一起把自己遇到的錯誤及解決方法寫出來。我是一個剛開始學java的大二學生,用的是《java開發實戰經典》。在寫p646的程式中一直報錯。特貼出來

先把程式碼發出來

public class ConnectionDemo02 {
//定義MySql的資料庫驅動程式
public static final String DBDRIVER="com.mysql.jdbc.Driver";   //錯誤,應改為com.mysql.cj.jdbc.Driver
//定義MySql資料庫的連結地址

public static final String DBURL="jdbc:mysql://localhost:3306/mldn";

//錯誤,應該改為:jdbc:mysql://localhost:3306/mldn?useSSL=false&serverTimezone=GMT";

//定義MySql資料庫的連線使用者名稱
public static final String DBUSER="root";
//定義MySql資料庫的連線密碼
public static final String DBPASS="12315";
public static void main(String[] args)
{
Connection conn=null;
try
{
Class.forName(DBDRIVER);
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
conn=DriverManager.getConnection(DBURL, DBUSER, DBPASS);
}catch(SQLException e)
{
e.printStackTrace();
}
System.out.println(conn);
try
{
conn.close();
}catch(SQLException e)
{
e.printStackTrace();
}

}

報錯是

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Sat May 05 09:24:57 GMT+08:00 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
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:127)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at 連線資料庫.ConnectionDemo02.main(ConnectionDemo02.java:28)
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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 6 more
null
Exception in thread "main" java.lang.NullPointerException
at 連線資料庫.ConnectionDemo02.main(ConnectionDemo02.java:36)

改正方法在原碼有