- JDBC 教程
- JDBC - 首頁
- JDBC - 簡介
- JDBC - SQL 語法
- JDBC - 環境
- JDBC - 示例程式碼
- JDBC - 驅動程式型別
- JDBC - 連線
- JDBC - 語句
- JDBC - 結果集
- JDBC - 資料型別
- JDBC - 事務
- JDBC - 異常
- JDBC - 批處理
- JDBC - 儲存過程
- JDBC - 資料流
- JDBC - RowSet
- JDBC - 複製資料庫
- JDBC - ACID 屬性
- JDBC - 連線池
- JDBC 示例
- JDBC - 建立資料庫
- JDBC - 選擇資料庫
- JDBC - 刪除資料庫
- JDBC - 建立表
- JDBC - 刪除表
- JDBC - 插入記錄
- JDBC - 選擇記錄
- JDBC - 更新記錄
- JDBC - 刪除記錄
- JDBC - WHERE 子句
- JDBC - LIKE 子句
- JDBC - 資料排序
- JDBC 有用資源
- JDBC - 常見問題解答
- JDBC - 快速指南
- JDBC - 有用資源
- JDBC - 討論
- 有用 - Java 教程
JDBC - 插入記錄
本章提供了一些示例,說明如何使用 JDBC 應用程式在表中插入記錄、插入多條記錄以及使用 select 查詢插入記錄。在執行以下示例之前,請確保您已準備好以下內容:
要執行以下示例,您可以將使用者名稱和密碼替換為您實際的使用者名稱和密碼。
您的 MySQL 或您正在使用的任何資料庫都已啟動並正在執行。
必要步驟
使用 JDBC 應用程式建立新資料庫需要以下步驟:
匯入包 - 需要包含用於資料庫程式設計的 JDBC 類所在的包。大多數情況下,使用import java.sql.*就足夠了。
註冊 JDBC 驅動程式 - 需要初始化一個驅動程式,以便您可以開啟與資料庫的通訊通道。
開啟連線 - 需要使用DriverManager.getConnection()方法建立一個 Connection 物件,該物件表示與資料庫伺服器的物理連線。
執行查詢 - 需要使用 Statement 型別的物件來構建和提交 SQL 語句,以將記錄插入表中。
清理環境 try with resources 會自動關閉資源。
示例:在表中插入記錄
在此示例中,我們有三個靜態字串,包含資料庫連線 URL、使用者名稱和密碼。現在,使用 DriverManager.getConnection() 方法,我們準備了一個數據庫連線。連線準備就緒後,我們使用 createStatement() 方法準備了一個 Statement 物件。下一步,我們準備了一個 SQL 字串,用於將記錄插入 REGISTRATION 表中,並透過呼叫 statement.executeUpdate() 方法將記錄插入資料庫。此後,我們更新了 SQL 字串以插入更多新記錄,並使用 executeUpdate() 方法逐個插入所有記錄。
如果在連線到資料庫時出現任何異常,catch 塊將處理 SQLException 並列印堆疊跟蹤。
複製並貼上以下示例到 JDBCExample.java 中,編譯並執行如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
public static void main(String[] args) {
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
) {
// Execute a query
System.out.println("Inserting records into the table...");
String sql = "INSERT INTO Registration VALUES (100, 'Zara', 'Ali', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration VALUES (101, 'Mahnaz', 'Fatma', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration VALUES (102, 'Zaid', 'Khan', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration VALUES(103, 'Sumit', 'Mittal', 28)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
現在讓我們編譯上述示例,如下所示:
C:\>javac JDBCExample.java C:\>
執行JDBCExample時,它會產生以下結果:
C:\>java JDBCExample Inserting records into the table... Inserted records into the table... C:\>
示例:在表中使用單個語句插入記錄
在此示例中,我們有三個靜態字串,包含資料庫連線 URL、使用者名稱和密碼。現在,使用 DriverManager.getConnection() 方法,我們準備了一個數據庫連線。連線準備就緒後,我們使用 createStatement() 方法準備了一個 Statement 物件。下一步,我們準備了一個 SQL 字串,用於一次性將多條記錄插入 sampledb4 表中,並透過呼叫 statement.execute() 方法將記錄插入資料庫。此後,我們運行了一個 select 查詢以讀取表中的所有記錄並打印出來。
如果在連線到資料庫時出現任何異常,catch 塊將處理 SQLException 並列印堆疊跟蹤。
複製並貼上以下示例到 JDBCExample.java 中,編譯並執行如下:
import java.sql.*;
// This class demonstrates use of multiple inserts within a single SQL
public class JDBCExample {
static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
static final String USER = "root";
static final String PASS = "guest123";
public static void main(String args[]) {
try{
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
stmt.execute("INSERT INTO sampledb4(id, name) VALUES(3, 'Sachin'), (4, 'Kishore')");
System.out.println("----- Successfully inserted into table sampledb4 ----\n\n");
System.out.println("Displaying records from sampledb4 table, showing inserted values");
System.out.println("---------------------------");
ResultSet rs = stmt.executeQuery("select * from sampledb4");
while(rs.next()){
System.out.println("id: " + rs.getInt(1));
System.out.println("name: " + rs.getString(2));
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
現在讓我們編譯上述示例,如下所示:
C:\>javac JDBCExample.java C:\>
執行JDBCExample時,它會產生以下結果:
C:\>java JDBCExample ----- Successfully inserted into table sampledb4 ---- Displaying records from sampledb4 table, showing inserted values --------------------------- id: 3 name: Sachin id: 4 name: Kishore C:\>
示例:使用 Select 語句在表中插入記錄
在此示例中,我們有三個靜態字串,包含資料庫連線 URL、使用者名稱和密碼。現在,使用 DriverManager.getConnection() 方法,我們準備了一個數據庫連線。連線準備就緒後,我們使用 createStatement() 方法準備了一個 Statement 物件。下一步,我們準備了一個 SQL 字串,用於使用 select 查詢將記錄插入 sampledb4 表中,並透過呼叫 statement.execute() 方法將記錄插入資料庫。此後,我們運行了一個 select 查詢以讀取表中的所有記錄並打印出來。
如果在連線到資料庫時出現任何異常,catch 塊將處理 SQLException 並列印堆疊跟蹤。
複製並貼上以下示例到 JDBCExample.java 中,編譯並執行如下:
import java.sql.*;
// This class demonstrates use of INSERT..SELECT SQL,
//where data is inserted in table using select from another table.
public class JDBCExample {
static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
static final String USER = "root";
static final String PASS = "guest123";
public static void main(String args[]) {
try{
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
// Data from students table (student id, first name) is inserted into sampledb4(id, name)
String ins_sel = "insert into sampledb4(id, name) select studentid,"
+" firstname from students where studentid > 1004";
stmt.executeUpdate(ins_sel);
ResultSet rs = stmt.executeQuery("select * from sampledb4 ");
System.out.println("Displaying records of table sampledb4/ Ids"
+" greater than 1004 are from students table");
System.out.println("--------------------------------------");
while(rs.next()){
System.out.print("id: " + rs.getInt(1));
System.out.println(" name: " + rs.getString(2));
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
現在讓我們編譯上述示例,如下所示:
C:\>javac JDBCExample.java C:\>
執行JDBCExample時,它會產生以下結果:
C:\>java JDBCExample Displaying records of table sampledb4/ Ids greater than 1004 are from students table -------------------------------------- id: 3 name: Sachin id: 4 name: Kishore id: 1005 name: Kishore id: 1006 name: Ganesh C:\>