使用JDBC串連MySQL資料庫

來源:互聯網
上載者:User

標籤:com   user   print   ima   rac   ring   sql   lips   pack   

Java資料庫連接(Java DataBase connectivity簡稱JDBC)下載JDBC驅動:https://dev.mysql.com/downloads/connector/j/Windows系統下載.zip檔案包,Linux平台下載tar.gz檔案包解壓後找gcfjmysql-connector-java-[version]-bin.jar包,JDBC通過這個檔案才可以正確的串連資料庫。 開啟Eclipse—— 右擊項目——Build Path——Add External JARs 瀏覽剛才下載的jar包。 Apply and Close 後,在項目目錄中可以找剛才添加的jar包 

 

接下來我們建立DBHelper簡單的資料訪問類,代碼如下:
package com.llt.demo; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement; public class DBHelper {       public static final String url = "jdbc:mysql://127.0.0.1/test";       // jdbc:mysql://host:port/database name       public static final String name = "com.mysql.jdbc.Driver";       public static final String user = "root";       // 資料庫登入使用者       public static final String password = "123456";        public Connection connection = null;       public PreparedStatement pst = null;        // 資料庫登入密碼       public DBHelper(String sql) {             try {                    Class.forName(name);// 指定連線類型                    connection = DriverManager.getConnection(url, user, password);// 建立串連                    pst = connection.prepareStatement(sql);// 執行Sql語句              } catch (Exception e) {                    e.printStackTrace();             }       }        public void close() {             try {                    this.connection.close();                    this.pst.close();             } catch (Exception e) {              }       }} 
View Code

 

然後建立一個簡單的類,測試連接一下MySQL資料庫
package com.llt.demo; import java.sql.ResultSet; public class test {        public static String sql = "";       public static DBHelper db = null;       public static ResultSet ret = null;        public static void main(String[] args) {             // TODO Auto-generated method stub             String sql = "select * from book";             db = new DBHelper(sql);             try {                    ret = db.pst.executeQuery();                    while (ret.next()) {                           int id = ret.getInt(1);                           String name = ret.getString(2);                           System.out.println("id:" + id + ",name:" + name);                     }                    // 使用完後將資料庫連接關閉                    ret.close();                    db.close();             } catch (Exception e) {                    e.printStackTrace();             }       }}
View Code

 

輸出MySQL資料庫book表中的內容:

 

使用JDBC串連MySQL資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.