如何使用JDBC API將記錄插入資料庫表?
A. 你可以使用 INSERT 查詢將記錄插入表中。
語法
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN); Or, INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN);
要使用 JDBC API 將記錄插入資料庫表中,您需要:
註冊驅動程式:使用 **DriverManager** 類的 **registerDriver()** 方法註冊驅動程式類。將驅動程式類名作為引數傳遞給它。
建立連線:使用 **DriverManager** 類的 **getConnection()** 方法連線到資料庫。將 URL(字串)、使用者名稱(字串)、密碼(字串)作為引數傳遞給它。
建立語句:使用 **Connection** 介面的 **createStatement()** 方法建立一個 Statement 物件。
執行查詢:使用 Statement 介面的 executeUpdate() 方法執行查詢。
示例
假設資料庫中有一個名為 customers 的表,其描述如下所示(此處應插入表結構描述)
+---------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+---------+-------+ | ID | int(11) | NO | PRI | NULL | | | NAME | varchar(20) | NO | | NULL | | | AGE | int(11) | NO | | NULL | | | SALARY | decimal(18,2) | YES | | NULL | | | ADDRESS | char(25) | YES | | NULL | | +---------+---------------+------+-----+---------+-------+
下面的 JDBC 程式建立與 MySQL 的連線,並在 customers 表中插入 12 條記錄(此處應插入程式碼示例)
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class InsertRecordsExample { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql:///mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Query to insert records String query = "INSERT INTO CUSTOMERS(" + "ID, Name, AGE, SALARY, ADDRESS) VALUES " + "(1, 'Amit', 25, 3000, 'Hyderabad'), " + "(2, 'Kalyan', 27, 4000, 'Vishakhapatnam'), " + "(3, 'Renuka', 30, 5000, 'Delhi'), " + "(4, 'Archana', 24, 1500, 'Mumbai')," + "(5, 'Koushik', 30, 9000, 'Kota')," + "(6, 'Hardik', 45, 6400, 'Bhopal')," + "(7, 'Trupthi', 33, 4360, 'Ahmedabad')," + "(8, 'Mithili', 26, 4100, 'Vijayawada')," + "(9, 'Maneesh', 39, 4000, 'Hyderabad')," + "(10, 'Rajaneesh', 30, 6400, 'Delhi')," + "(11, 'Komal', 29, 8000, 'Ahmedabad')," + "(12, 'Manyata', 25, 5000, 'Vijayawada')"; int i = stmt.executeUpdate(query); System.out.println("Rows inserted: "+i); } }
輸出
Connection established...... Rows inserted: 12
如果您使用 select 語句驗證 customers 表的內容,您可以在其中找到插入的記錄,例如:
mysql> select * from customers; +----+-----------+------+---------+----------------+ | ID | NAME | AGE | SALARY | ADDRESS | +----+-----------+------+---------+----------------+ | 1 | Amit | 25 | 3000.00 | Hyderabad | | 2 | Kalyan | 27 | 4000.00 | Vishakhapatnam | | 3 | Renuka | 30 | 5000.00 | Delhi | | 4 | Archana | 24 | 1500.00 | Mumbai | | 5 | Koushik | 30 | 9000.00 | Kota | | 6 | Hardik | 45 | 6400.00 | Bhopal | | 7 | Trupthi | 33 | 4360.00 | Ahmedabad | | 8 | Mithili | 26 | 4100.00 | Vijayawada | | 9 | Maneesh | 39 | 4000.00 | Hyderabad | | 10 | Rajaneesh | 30 | 6400.00 | Delhi | | 11 | Komal | 29 | 8000.00 | Ahmedabad | | 12 | Manyata | 25 | 5000.00 | Vijayawada | +----+-----------+------+---------+----------------+ 12 rows in set (0.06 sec)
廣告