java連線MySQL出現版本錯誤及連線資料庫程式碼
阿新 • • 發佈:2018-12-25
今天eclipse連線mysql遇到不少問題。
我用的環境如下(錯誤的情況下):
eclipse 4.7;
jdk1.7(Java project);
mysql5.6;
mysql-connector-java-8.0.12.jar(需要匯入);
部分連線語句(錯誤的部分):
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test ";
1 版本錯誤:Unsupported major.minor version 52.0
JDK版本在官方對應的版本號列表如下(引用https://blog.csdn.net/zixiao217/article/details/52818658):
此錯誤將
JDK 8.0 = 52,
JDK 7.0 = 51,
JDK 6.0 = 50,
JDK 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45
將jdk 修改成1.8後這個錯誤消失
2 連線驅動driver錯誤
錯誤提示:
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.
加入‘cj.’修改為此錯誤消失:
String driver = "com.mysql.cj.jdbc.Driver";
3 時區沒有設定
錯誤提示:
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:832) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at mysqljdbc1217.main.main(main.java:29) 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:61) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128) at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2236) at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2260) at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1314) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:963) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822) ... 6 more
修改 :
String url = "jdbc:mysql://localhost:3306/test "+ "?serverTimezone=GMT%2B8";
錯誤全部消失。
全部程式碼如下:
package mysqljdbc1217;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class main {
public static void main(String[] args) {
//宣告Connection物件
Connection con;
//驅動程式名
String driver = "com.mysql.cj.jdbc.Driver";
//URL指向要訪問的資料庫名mydata
String url = "jdbc:mysql://localhost:3306/test "+ "?serverTimezone=GMT%2B8";
//MySQL配置時的使用者名稱
String user = "root";
//MySQL配置時的密碼
String password = "11111";
//遍歷查詢結果集
try {
//載入驅動程式
System.out.println("執行結果如下所示:");
Class.forName(driver);
System.out.println("執行結果如下所示:");
//1.getConnection()方法,連線MySQL資料庫!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.建立statement類物件,用來執行SQL語句!!
Statement statement = con.createStatement();
//要執行的SQL語句
String sql = "select * from temporpary_data";
//3.ResultSet類,用來存放獲取的結果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("執行結果如下所示:");
System.out.println("-----------------");
System.out.println("姓名" + "\t" + "職稱");
System.out.println("-----------------");
String job = null;
String id = null;
/* while(rs.next()){
//獲取stuname這列資料
job = rs.getString("job");
//獲取stuid這列資料
id = rs.getString("ename");
//輸出結果
System.out.println(id + "\t" + job);
}
rs.close();
con.close();*/
} catch(ClassNotFoundException e) {
//資料庫驅動類異常處理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//資料庫連線失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("資料庫資料成功獲取!!");
}
}
}