eclipse 連接 mysql

連接時

報錯:
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.
原因:
提示信息表明數據庫驅動com.mysql.jdbc.Driver'已經被棄用了、應當使用新的驅動com.mysql.cj.jdbc.Driver

報錯: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.
原因:因爲mysql默認的時區和本地時區不一致導致的

 


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


public class Test2 {
		public static void main(String[] args) throws Exception  {//加throws Exception的作用是拋出異常
			//mis:是要連接數據庫的地址,就是我們在MySQL裏創建的數據庫
			//serverTimezone=GMT%2B8:是設置當前的系統時區
			
			String url = "jdbc:mysql:// localhost:3306/mis?serverTimezone=GMT%2B8";
	 
			//加載JDBC-MySQL數據庫驅動
			try {
				Class.forName( "com.mysql.cj.jdbc.Driver");
			}catch(Exception e){}
			

			Connection con;
			
			try {
				//建立連接
				con = DriverManager.getConnection(url,"root","000000");
				System.out.println("數據庫連接成功");
			}catch(SQLException e) {
				System.out.println("數據庫連接失敗");
				System.out.println(e);
			}
		}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章