《JAVA資料庫編程》

來源:互聯網
上載者:User

標籤:

  首先,我們需要瞭解JDBC的概念。

  JDBC(Java Database Connectivity)是Java中提供的一套資料庫編程API,它定義了一套用來訪問資料庫的標準Java類庫(位於java.sql和javax.sql包中)。利用JDBC,我們可以用Java編寫程式,實現與特定的資料庫連接,向資料庫發送SQL語句,實現對資料庫的特定操作,並對資料庫返回的結果進行處理。

  JDBC編程一般包括如下六個步驟:

    1.根據應用程式所用的資料庫,選擇JDBC驅動程式類型。

    2.串連到資料庫,得到Connection對象。

    3.通過Connection建立Statement對象。

    4.使用Stateme對象提交SQL語句。

    5.操作結果集。

    6.回收資料庫資源。

  代碼如下:

package sqlpractice;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class First {    /**     * @param args     */    public static void main(String[] args) {        Connection conn = null; // 連線物件        Statement stat = null; // Statement對象        ResultSet resu = null; // 查詢結果集        /* JDBC 開發步驟 */        // 第一步 將驅動程式套件添加到Build path中        // 第二步 載入驅動        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            e.printStackTrace();        }        // 第三步 建立connection對象        String url = "jdbc:mysql://localhost:3306/jdbc";        // 使用者名稱        String user = "root";        // 密碼        String password = "****";        try {            conn = DriverManager.getConnection(url, user, password);        } catch (SQLException e) {            e.printStackTrace();        }        // 第四步 建立Statement對象        try {            stat = conn.createStatement();        } catch (SQLException e) {            e.printStackTrace();        }        // 第五步 發送SQL語句        String sql = "SELECT * FROM user";        try {            resu = stat.executeQuery(sql);        } catch (SQLException e) {            e.printStackTrace();        }        // 第六步 處理結果        try {            while (resu.next()) {                System.out.println("\n\t使用者ID:" + resu.getString("id") + "使用者名稱:"                        + resu.getString("username") + "密碼:"                        + resu.getString("password"));            }        } catch (SQLException e) {            e.printStackTrace();        }        // 第七步 釋放資源        if (conn != null) {            try {                conn.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (stat != null) {            try {                stat.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (resu != null) {            try {                resu.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }}

 

《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.