1. 程式人生 > >tomcat9.0+eclipse ee+jdbc配置

tomcat9.0+eclipse ee+jdbc配置

選擇這個

2.解壓它,拷貝mysql-connector-java-8.0.12.jar檔案放到工程中我的是放在tomcat/lib/    下

,並匯入路徑

方法:右擊工程名->Build Path->Configure Build Path,選擇Add External JAR… 找到mysql-connector-java-8.0.12.jar所在的位置,然後將驅動包載入到專案中, 

像上面這樣

然後將jdbc新增到web專案中

把命令列cmd切換到mysql的安裝目錄下的bin

1.      mysql -u root -p

2.  輸入密碼

3.    建立database:      create database RUNOOB;

4.   use   RUNOOB;

5.               CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' COMMENT '站點名稱',`url` varchar(255) NOT NULL DEFAULT '',`alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',  `country` char(10) NOT NULL DEFAULT '' COMMENT '國家',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

6.開啟mysql服務

成功測試java程式碼:

  import java.sql.*;   public class MySQLDemo {       // JDBC 驅動名及資料庫 URL     static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";       //"jdbc:mysql://localhost:3306/RUNOOB";     static final String DB_URL =      "jdbc:mysql://localhost:3306/RUNOOB?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true";     // 資料庫的使用者名稱與密碼,需要根據自己的設定     static final String USER = "root";     static final String PASS = "123456";       public static void main(String[] args) {         Connection conn = null;         Statement stmt = null;         try{             // 註冊 JDBC 驅動             Class.forName("com.mysql.jdbc.Driver");                      // 開啟連結             System.out.println("連線資料庫...");             conn = DriverManager.getConnection(DB_URL,USER,PASS);                      // 執行查詢             System.out.println(" 例項化Statement物件...");             stmt = conn.createStatement();             String sql;             sql = "SELECT id, name, url FROM websites";             ResultSet rs = stmt.executeQuery(sql);                      // 展開結果集資料庫             while(rs.next()){                 // 通過欄位檢索                 int id  = rs.getInt("id");                 String name = rs.getString("name");                 String url = rs.getString("url");                      // 輸出資料                 System.out.print("ID: " + id);                 System.out.print(", 站點名稱: " + name);                 System.out.print(", 站點 URL: " + url);                 System.out.print("\n");             }             // 完成後關閉             rs.close();             stmt.close();             conn.close();         }catch(SQLException se){             // 處理 JDBC 錯誤             se.printStackTrace();         }catch(Exception e){             // 處理 Class.forName 錯誤             e.printStackTrace();         }finally{             // 關閉資源             try{                 if(stmt!=null) stmt.close();             }catch(SQLException se2){             }// 什麼都不做             try{                 if(conn!=null) conn.close();             }catch(SQLException se){                 se.printStackTrace();             }         }         System.out.println("Goodbye!");      } }

錯誤提示:

遇到了驅動問題、SSL安全訪問的問題和時區問題。  報錯1:

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. 1 高版本的驅動已經由

"com.mysql.jdbc.Driver" 1 變更為

private static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver"; 1 報錯2:

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. 建立連線失敗,檢視控制檯異常資訊 1 2 這個是安全連線的問題,在sql連線字串中,新增關於不使用SSL訪問資料資料庫的說明:useSSL=false。

報錯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. 1 這個是時區問題,在訪問字串中新增關於時區的設定說明:serverTimezone=UTC。

完整的資料庫訪問字串為:

private static final String DB_URL = "jdbc:mysql://localhost:3306/jdbc_example?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";