標籤:
轉載自:http://www.jb51.net/article/31876.htm
1.前提是MyEclipse已經能正常開發Java工程
2.安裝MySQL
個人使用的是版本是 mysql-5.0.22-win32.zip
網址:http://www.mysql.com/downloads/mysql/#downloads
3.下載JDBC驅動
個人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解壓縮之後其中的 mysql-connector-java-5.1.22-bin.jar
網址:http://www.mysql.com/downloads/connector/j/
4.代碼測試
1 package ts.jsj.lyh; 2 3 import java.sql.*; 4 5 /** */ 6 /** 7 * 使用JDBC串連資料庫MySQL的過程 DataBase:JSJ, table:student; 8 * 9 * @author DuChangfeng 2008 09 1810 */11 public class JDBCTest {12 13 public static Connection getConnection() throws SQLException,14 java.lang.ClassNotFoundException {15 // 第一步:載入MySQL的JDBC的驅動16 Class.forName("com.mysql.jdbc.Driver");17 18 // 取得串連的url,能訪問MySQL資料庫的使用者名稱,密碼;jsj:資料庫名19 String url = "jdbc:mysql://localhost:3306/mydata";20 String username = "root";21 String password = "";22 23 // 第二步:建立與MySQL資料庫的串連類的執行個體24 Connection con = DriverManager.getConnection(url, username, password);25 return con;26 }27 28 public static void main(String args[]) {29 try {30 // 第三步:擷取串連類執行個體con,用con建立Statement對象類執行個體 sql_statement31 Connection con = getConnection();32 Statement sql_statement = con.createStatement();33 34 /** */35 /************ 對資料庫進行相關操作 ************/36 // 如果同名資料庫存在,刪除37 // sql_statement.executeUpdate("drop table if exists student");38 // 執行了一個sql語句產生了一個名為student的表39 // sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default ‘name‘, math int not null default 60, primary key (id) ); ");40 // 向表中插入資料41 // sql_statement.executeUpdate("insert student values(1, ‘liying‘, 98)");42 // sql_statement.executeUpdate("insert student values(2, ‘jiangshan‘, 88)");43 // sql_statement.executeUpdate("insert student values(3, ‘wangjiawu‘, 78)");44 // sql_statement.executeUpdate("insert student values(4, ‘duchangfeng‘, 100)");45 // ---以上操作不實用,但是列出來作為參考---46 47 // 第四步:執行查詢,用ResultSet類的對象,返回查詢的結果48 String query = "select * from student";49 ResultSet result = sql_statement.executeQuery(query);50 /** */51 /************ 對資料庫進行相關操作 ************/52 53 System.out.println("Student表中的資料如下:");54 System.out.println("------------------------");55 System.out.println("學號" + " " + "姓名" + " " + "資料成績 ");56 System.out.println("------------------------");57 58 // 對獲得的查詢結果進行處理,對Result類的對象進行操作59 while (result.next()) {60 int number = result.getInt("sno");61 String name = result.getString("sname");62 String mathScore = result.getString("sgrade");63 // 取得資料庫中的資料64 System.out.println(" " + number + " " + name + " " + mathScore);65 }66 67 // 關閉串連和聲明68 sql_statement.close();69 con.close();70 71 } catch (java.lang.ClassNotFoundException e) {72 // 載入JDBC錯誤,所要用的驅動沒有找到73 System.err.print("ClassNotFoundException");74 // 其他錯誤75 System.err.println(e.getMessage());76 } catch (SQLException ex) {77 // 顯示資料庫連接錯誤或查詢錯誤78 System.err.println("SQLException: " + ex.getMessage());79 }80 }81 82 }
加上幾點個人認為需要注意的地方:
1)關於mysql-connector-java-5.1.22-bin.jar 的存放位置。在MyEclipse具體的java工程中建立一存放jar 包的檔案夾(如 lib),將mysql-connector-java-5.1.22-bin.jar 複製到檔案夾中,選中jar包右擊--->Build Path--->Add To Build Path,即可。
若出現
ClassNotFoundExceptioncom.mysql.jdbc.Driver
的提示,則正是由於缺少匯入jar包所造成的。
2)如果已經對MySQL的使用很熟悉,則可忽略這條。個人在測試連接時,老是出現這樣的異常提示:
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
這正是由於個人對MySQL使用不熟悉,對MySQL進行了諸多嘗試性的操作,不知何時無意中將MySQL的服務(如果在安裝MySQL時沒有更改的話,預設服務名就是MySQL)關閉,解決方案開啟此服務即可。控制台--->管理工具--->服務--->MySQL--->選擇啟用。
3)在使用上面的代碼測試時,需要更改的地方有:
//MySQL資料庫的使用者名稱,密碼,資料庫名
1 String url = "jdbc:mysql://localhost:3306/jsj"; 2 String username = "root"; 3 String password = "111";
以及具體基本表中的所要查詢的欄位名:
1 int number = result.getInt("sno"); 2 String name = result.getString("sname"); 3 String mathScore = result.getString("sgrade");
MyEclipse通過JDBC串連MySQL資料庫基本介紹