English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
本章节我们为大家介绍 Java 如何使用 JDBC 连接 MySQL 数据库。
Java 连接 MySQL 需要驱动包,最新版下载地址为:http://dev.mysql.com/downloads/connector/j/,解压后得到 jar 库文件,然后在对应的项目中导入该库文件。
你可以下载本站提供的 jar 包:mysql-connector-java-5.1.39-bin.jar
本示例使用的是 Eclipse,导入 jar 包:
MySQL 8.0 以上版本的数据库连接有所不同:
1、MySQL 8.0 以上版本驱动包版本 mysql-connector-java-8.0.16.jar。
2、com.mysql.jdbc.Driver 更换为 com.mysql.cj.jdbc.Driver。
MySQL 8.0 以上版本不需要建立 SSL 连接的,需要显示关闭。
allowPublicKeyRetrieval=true 允许客户端从服务器获取公钥。
最后还需要设置 CST。
加载驱动与连接数据库方式如下:
Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql:")//localhost:3306/test_demo?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC","root","password");
Erstellen Sie Testdaten3Erstellen wir in MySQL w
CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' COMMENT 'Seitenname', `url` varchar(255) NOT NULL DEFAULT '', `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa-Rang', `country` char(10) NOT NULL DEFAULT '' COMMENT 'Land', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
Einige Daten einfügen:
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', ''1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', ''13', 'CN'), ('3', 'w3codebox', 'http://de.oldtoolbag.com5892', ''4', '微博', 'http://weibo.com/', ''20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', ''3', 'USA');
Die Datenbanktabelle wird wie folgt angezeigt:
Der folgende Beispiel verwendet die JDBC-Verbindung zur MySQL-Datenbank. Beachten Sie, dass einige Daten wie Benutzername und Passwort entsprechend Ihrer Entwicklungs環境 konfiguriert werden müssen:
package com.w3codebox.test; import java.sql.*; public class MySQLDemo { // MySQL 8.0 Die folgenden Versionen - JDBC 驅動名及數據庫 URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/w3codebox"; // MySQL 8.0 以上版本 - JDBC 驅動名及數據庫 URL //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //static final String DB_URL = "jdbc:mysql://localhost:3306/w3codebox?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; // 數據庫的用戶名與密碼,需要根據自己的設置 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(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(", Site URL: " + url); System.out.print("\n"); } // Nach Abschluss schließen rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // Verarbeitung von JDBC-Fehlern se.printStackTrace(); } // Verarbeitung von Class.forName-Fehlern e.printStackTrace(); } // Ressourcen schließen try{ catch(SQLException se }2{ }// Nichts tun try{ if(conn != null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); } }
Die Ausgabe der folgenden Beispiele ist wie folgt: