標籤:
JDBC(Java Database Connectivity):是java中提供的一套資料庫編程API,它定義了一套用來訪問資料庫的標準Java類庫(位於java.sql和javax.sql包中)。利用JDBC,可以 用java編寫程式,實現與特定的資料庫連接,向資料庫發送SQL語句,實現對資料庫的特定操作,並對資料庫返回結果進行處理。
1.JDBC編程六個步驟 :(1).根據應用程式所用資料庫,選擇JDBC驅動程式類型。
(2).串連到資料庫,得到Connection對象。
(3).通過Connection建立Statement對象。
(4).使用Statement對象提交SQL語句。
(5).操作結果集。
(6).回收資料庫資源。
2.選定JDBC驅動程式類型 (1).LDBC-ODBC橋加ODBC 驅動程式
(2).本地API和部分基於Java的驅動程
(3).為資料庫中介軟體實現的純Java驅動程式
(4).本地協議純Java驅動程式(效能最好)
3.JDBC串連程式碼範例:
package com;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;/** * JDBC輔助類 * * @author * */public class JdbcUtil { /* 最好使用設定檔儲存 */ private static String driver; // 驅動 private static String url; // 連接字串 private static String user; // 使用者名稱 private static String password; // 密碼 private static Connection conn; // Connection對象 private static PreparedStatement pstmt; // PreparedStatement對象 private static ResultSet rs; // ResultSet對象 /* 載入資料庫驅動 */ static { loadProperties(); try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } private JdbcUtil() { } /** * 讀取設定檔 */ private static void loadProperties() { Properties properties = new Properties(); InputStream in = Thread.currentThread().getClass() .getResourceAsStream("/com/lovo/day2/jdbc.properties"); try { properties.load(in); } catch (IOException e) { throw new RuntimeException(e); } driver = properties.getProperty("driver"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); } /** * 擷取資料庫連接 * * @return 連線物件 */ public static Connection getConnection() { try { return conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 釋放ResultSet資源 * * @param rs */ public static void close(ResultSet rs) { try { if (null != rs) rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 釋放ResultSet資源 * * @param rs */ public static void close(Statement stmt) { try { if (null != stmt) stmt.close(); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 釋放ResultSet資源 * * @param rs */ public static void close(Connection conn) { try { if (null != conn) conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 釋放所有資源 */ public static void close() { try { if (null != rs && !rs.isClosed()) { rs.close(); } rs = null; } catch (SQLException e) { throw new RuntimeException(e); } finally { try { if (null != pstmt && !pstmt.isClosed()) { pstmt.close(); } pstmt = null; } catch (SQLException e) { throw new RuntimeException(e); } finally { try { System.out.println(conn); if (null != conn && !conn.isClosed()) { conn.close(); } conn = null; } catch (SQLException e) { throw new RuntimeException(e); } } } } /** * 執行增刪改 * * @param sql * 待執行的SQL語句 * @param params * SQL語句中?預留位置的參數 * @return 受影響的行數 */ public static int executeUpdate(String sql, Object... params) { try { if (null == conn) getConnection(); pstmt = conn.prepareStatement(sql); if (null != params) { // 為SQL語句中?預留位置設定參數 for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } } return pstmt.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 實現查詢 * * @param sql * 待執行的查詢SQL語句 * @param params * SQL語句中?預留位置的參數 * @return 查詢結果集ResultSet對象 */ public static ResultSet executeQuery(String sql, Object... params) { try { if (null == conn) getConnection(); pstmt = conn.prepareStatement(sql); if (null != params) { // 為SQL語句中?預留位置設定參數 for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } } return rs = pstmt.executeQuery(); } catch (SQLException e) { throw new RuntimeException(e); } }}
Java資料庫編程