如何使用JDBC API将记录插入数据库的表中?

答:您可以使用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(字符串),用户名(字符串),密码(字符串)作为参数传递给它。

  • Create Statement:使用Connection 接口的createStatement()方法创建一个Statement对象。

  • 执行查询:使用executeUpdate()Statement接口的方法执行查询。

示例

假设我们在数据库中有一个名为客户的表,其描述如下所示:

+---------+---------------+------+-----+---------+-------+
| 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 {
      //注册驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //获得连接
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //创建语句
      Statement stmt = con.createStatement();
      //查询插入记录
      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)