Java資料庫編程

來源:互聯網
上載者:User

標籤:

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資料庫編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.