JAVA使用JDBC串連MySQL資料庫

來源:互聯網
上載者:User

標籤:

首先要下載Connector/J地址:http://www.mysql.com/downloads/connector/j/

這是MySQL官方提供的串連方式:

解壓後得到jar庫檔案,需要在工程中匯入該庫檔案

我是用的是Eclipse:



 

 

 JAVA串連MySQL稍微繁瑣,所以先寫一個類用來開啟或關閉資料庫:

DBHelper.java

Java代碼  
  1. package com.hu.demo;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.SQLException;  
  7.   
  8. public class DBHelper {  
  9.     public static final String url = "jdbc:mysql://127.0.0.1/student";  
  10.     public static final String name = "com.mysql.jdbc.Driver";  
  11.     public static final String user = "root";  
  12.     public static final String password = "root";  
  13.   
  14.     public Connection conn = null;  
  15.     public PreparedStatement pst = null;  
  16.   
  17.     public DBHelper(String sql) {  
  18.         try {  
  19.             Class.forName(name);//指定連線類型  
  20.             conn = DriverManager.getConnection(url, user, password);//擷取串連  
  21.             pst = conn.prepareStatement(sql);//準備執行語句  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.   
  27.     public void close() {  
  28.         try {  
  29.             this.conn.close();  
  30.             this.pst.close();  
  31.         } catch (SQLException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  

再寫一個Demo.java來執行相關查詢操作

Demo.java

Java代碼  
  1. package com.hu.demo;  
  2.   
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5.   
  6. public class Demo {  
  7.   
  8.     static String sql = null;  
  9.     static DBHelper db1 = null;  
  10.     static ResultSet ret = null;  
  11.   
  12.     public static void main(String[] args) {  
  13.         sql = "select *from stuinfo";//SQL語句  
  14.         db1 = new DBHelper(sql);//建立DBHelper對象  
  15.   
  16.         try {  
  17.             ret = db1.pst.executeQuery();//執行語句,得到結果集  
  18.             while (ret.next()) {  
  19.                 String uid = ret.getString(1);  
  20.                 String ufname = ret.getString(2);  
  21.                 String ulname = ret.getString(3);  
  22.                 String udate = ret.getString(4);  
  23.                 System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate );  
  24.             }//顯示資料  
  25.             ret.close();  
  26.             db1.close();//關閉串連  
  27.         } catch (SQLException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.   
  32. }  

 測試資料庫是在上一章 中建立的,所以直接查詢:

JAVA使用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.