JDBC的使用及操作過程

JDBC的使用及操作過程

概念:

JDBC API是一個Java API,可以訪問任何類型表列數據,特別是存儲在關係數據庫中的數據。JDBC代表Java數據庫連接。

JDBC庫中所包含的API通常與數據庫使用於:

  • 連接到數據庫

  • 創建SQL或MySQL語句

  • 在數據庫中執行SQL或MySQL查詢

  • 查看和修改數據庫中的數據記錄

使用JDBC的前提條件

1.配置環境變量

請確認您已完成以下設置:

  • JAVA(JDK)安裝

  • 數據庫系統的安裝(如:MySQL的安裝)

除上述者外環境配置外,還需要建立一個數據庫,爲本程項目作爲測試使用。假設創建一個數據庫:test,在這個數據庫上創建一張表:employees

2.創建JDBC程序應用

2.1 導入包

在程序中包含數據庫編程所需的JDBC類。大多數情況下,使用 import java.sql.* 就足夠了,如下所示:

//STEP 1. Import required packages
import java.sql.*;

2.2 註冊JDBC的驅動程序

需要初始化驅動程序,這樣就可以打開與數據庫的通信。以下是代碼片段實現這一目標:


//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

2.3 創建一個連接

通過DriverManager類的getConnection方法建立連接時,需要注意getConnection會拋出SQLException異常,需要在try/catch塊 中捕獲


static final String USER = "root";
static final String PASS = "pwd123456";
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);

2.4 執行增刪查找的功能

2.4.1 Insert


private static int insert(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setString(1, student.getName());
        pstmt.setString(2, student.getSex());
        pstmt.setString(3, student.getAge());
        i = pstmt.executeUpdate();
        pstmt.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}

2.4.2 UpData


private static int update(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        i = pstmt.executeUpdate();
        System.out.println("resutl: " + i);
        pstmt.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}

2.4.3 select


private static Integer getAll() {
    Connection conn = getConn();
    String sql = "select * from students";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement)conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        int col = rs.getMetaData().getColumnCount();
        System.out.println("============================");
        while (rs.next()) {
            for (int i = 1; i <= col; i++) {
                System.out.print(rs.getString(i) + "\t");
                if ((i == 2) && (rs.getString(i).length() < 8)) {
                    System.out.print("\t");
                }
             }
            System.out.println("");
        }
            System.out.println("============================");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

2.4.4 delete


private static int delete(String name) {
    Connection conn = getConn();
    int i = 0;
    String sql = "delete from students where Name='" + name + "'";
    PreparedStatement pstmt;
    try {
        pstmt = (PreparedStatement) conn.prepareStatement(sql);
        i = pstmt.executeUpdate();
        System.out.println("resutl: " + i);
        pstmt.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}


發佈了33 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章