mysql-connector-java-5.1.22下載


java連接mysql時,需要安裝驅動。如果未安裝,會出現找不到“com.mysql.jdbc.Driver”的錯誤。

最新版驅動是:mysql-connector-java-5.1.22

下載地址:http://pan.baidu.com/share/link?shareid=64178&uk=2585386604

安裝驅動程序:

1、下載jdbc的驅動,解壓到任一位置中

2、打開eclipse,找到再在windows->preferences->java->installed jres

3、單擊Sun JDk….,然后單擊edit

4、點擊add external jars,選擇壓縮包中的mysql-connector-java-5.1.22-bin.jar

5、點擊finish

使用下面的程序測試數據庫連接:

import java.sql.*;
public class JDBCTest {
public static void main(String[] args){
// 驅動程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的數據庫名game
String url = "jdbc:mysql://127.0.0.1:3306/game";
// MySQL配置時的用戶名
String user = "root";
// MySQL配置時的密碼
String password = "root";
try {
// 加載驅動程序
Class.forName(driver);
// 連續數據庫
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "select id,username from user_index order by id desc limit 0,5";
// 結果集
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("執行結果如下所示:");
System.out.println("-----------------");
System.out.println(" id" + "\t" + " 用戶名");
System.out.println("-----------------");
String name = null;
while(rs.next()) {
// 選擇username這列數據
name = rs.getString("username");
// 首先使用ISO-8859-1字符集將name解碼為字節序列並將結果存儲新的字節數組中。
// 然后使用GB2312字符集解碼指定的字節數組
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
// 輸出結果
System.out.println(rs.getString("id") + "\t" + name);
}
rs.close();
conn.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM