如何使用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)
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP