1. 程式人生 > >Java 通過JDBC連線並操作Mysql資料庫

Java 通過JDBC連線並操作Mysql資料庫

使用Java連線資料庫需要使用JDBC驅動。JDBC(Java Data Base Connectivity,java資料庫連線)是一種用於執行SQL語句的Java API,可以為多種關係資料庫提供統一訪問,它由一組用Java語言編寫的類和介面組成。以下是使用方法 一、安裝配置好mysql 二、下載驅動包 : http://dev.mysql.com/downloads/connector/j/ 解壓,得到jar檔案。(當前版本得到的是mysql-connector-java-5.1.44-bin.jar)。將該檔案複製到Java工程目錄Java Resources/Libraries/ 下。 三、連線資料庫(假設存在資料庫databaseweb, 其中有一表tb_person) 3.1 註冊驅動 Class.forName(“com.mysql.jdbc.Driver”); 或者 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 3.2 獲取連線 Connection conn = (Connection) DriverManager.getConnection(URL,USER,PASS); URL為資料庫地址(含埠號),後兩個引數為資料庫使用者名稱和密碼 3.3 獲取statement Statement stat = conn.createStatement(); 或PreparedStatement preStat = conn.prepareStatement(sql) PreparedStatement繼承自Statement,比Statement的效率更高,並且可以防止SQL注入,使用較多; PreparedStatement()傳入的引數sql含有未知量,須進一步設定。 3.4 執行SQL並返回結果 執行select使用executeQuery(),返回ResultSet型別; 執行insert、update、delete等使用executeUpdate(),返回影響的行數(int型別);

3.5 批量處理 對於Statement,如下:

.......
stat.addBatch(sql1);
stat.addBatch(sql2);
.....
stat.addBatch(sqln);  //執行n條語句
int[] result = stmt.executeBatch();  //陣列長度為n
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

對於PreparedStament,如果sql不帶引數,則和Statement一樣。如果帶引數,如下

for(int i = 0; i < 5; i++){ //批量處理5條
  preStat.setString(1,"name"+i);
  preStat.setString
(2,"sex"+i); preStat.setInt(3,"age"+i); preStat.addBatch(); } int[] result = stmt.executeBatch();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.5 處理結果集 用select查詢返回ResultSet型別,該型別的物件常用方法有: rs.next() // 向後移動 rs.last() //定位到末端 rs.getTnt(name) re.getString(name)

示例程式碼如下:

import java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement; import com.mysql.jdbc.Connection; public class Test { static final String DB_URL = "jdbc:mysql://localhost:3306/databaseweb"; // MySQL的JDBC URL編寫方式:jdbc:mysql://主機名稱:連線埠/資料庫的名稱 static final String USER = "root"; static final String PASS = "123"; public static void main(String[] args) throws SQLException,Exception{ Connection conn = null; Statement stat = null; // 註冊驅動 Class.forName("com.mysql.jdbc.Driver"); // 建立連結 conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS); // 執行查詢 stat = conn.createStatement(); String sql = "SELECT * FROM tb_person"; ResultSet rs = stat.executeQuery(sql); // 輸出查詢結果 while(rs.next()){ System.out.print(rs.getInt("id")+","); System.out.print(rs.getString("name")+","); System.out.print(rs.getString("sex")+","); System.out.print(rs.getInt("age")); System.out.print("\n"); } // 關閉 try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stat != null) { stat.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60