如何使用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)

更新于: 2019-7-30

1K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.