如何使用JDBC API向現有表新增新列?


您可以使用ALTER TABLE命令向表新增新列。

語法

ALTER TABLE table_name ADD column_name datatype;

假設資料庫中有一個名為Sales的表,包含5列,分別是ProductName、CustomerName、DispatchDate、DeliveryTime、Price和Location,如下所示

+-------------+--------------+--------------+--------------+-------+----------------+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+-------------+--------------+--------------+--------------+-------+----------------+
| Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      |
| Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000  | Vishakhapatnam |
| Mouse       | Puja         | 2019-03-01   | 04:26:38     | 3000  | Vijayawada     |
| Mobile      | Vanaja       | 2019-03-01   | 04:26:35     | 9000  | Vijayawada     |
| Headset     | Jalaja       | 2019-04-06   | 05:19:16     | 6000  | Vijayawada     |
+-------------+--------------+--------------+--------------+-------+----------------+

以下JDBC程式建立與MySQL資料庫的連線,並向Sales表新增一個名為ID的新列並填充它。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class AddingColumn {
   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 alter the table
      String query = "ALTER TABLE Sales ADD ID INT NOT NULL";
      //Executing the query
      stmt.executeUpdate(query);
      System.out.println("Column added......");
      //inserting values in to the added column
      PreparedStatement pstmt = con.prepareStatement("UPDATE sales SET id = ? where ProductName = ");
      pstmt.setInt(1, 1);
      pstmt.setString(2,"Key-Board");
      pstmt.executeUpdate();

      pstmt.setInt(1, 2);
      pstmt.setString(2,"Earphones");
      pstmt.executeUpdate();

      pstmt.setInt(1, 3);
      pstmt.setString(2,"Mouse");
      pstmt.executeUpdate();

      pstmt.setInt(1, 4);
      pstmt.setString(2,"Mobile");
      pstmt.executeUpdate();

      pstmt.setInt(1, 5);
      pstmt.setString(2,"Headset");
      pstmt.executeUpdate();

      System.out.println("Values inserted......");
   }
}

輸出

Connection established......
Column added......
Values inserted in the added column

由於我們添加了一列,如果您使用SELECT命令檢索Sales表的內容,您可以看到7列,其中新增一列名為id,如下所示

mysql> select * from Sales;
+-------------+--------------+--------------+--------------+-------+----------------+----+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       | ID |
+-------------+--------------+--------------+--------------+-------+----------------+----+
| Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      | 1  |
| Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000  | Vishakhapatnam | 2  |
| Mouse       | Puja         | 2019-03-01   | 04:26:38     | 3000  | Vijayawada     | 3  |
| Mobile      | Vanaja       | 2019-03-01   | 04:26:35     | 9000  | Chennai        | 4  |
| Headset     | Jalaja       | 2019-03-01   | 05:19:16     | 6000  | Delhi          | 5  |
+-------------+--------------+--------------+--------------+-------+----------------+----+
5 rows in set (0.00 sec)

更新於: 2019年7月30日

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.