如何使用 JDBC API 向现有表添加新列?
jdbcjava 8object oriented programmingprogramming
您可以使用 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 { //注册驱动程序 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 = "ALTER TABLE Sales ADD ID INT NOT NULL"; //执行查询 stmt.executeUpdate(query); System.out.println("Column Added......"); //将值插入到添加的列中 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)