如何使用 JDBC 重新排序表中的列?
你可以使用 ALTER TABLE 命令来重新排序 MySQL 数据库中表的列。
语法
ALTER TABLE table_name MODIFY column_name datatype AFTER another_column
假定我们在数据库中有一个名为dispatches_data的表,其中有 7 列,即 ProductName、CustomerName、DispatchDate、DeliveryTime、Price、Location 和 ID,其描述如下 -
//Retrieving the Time object Time timeObj = rs.getTime("DeliveryTime"); //Converting the Time object to String format String time = timeObj.toString();
示例
让我们使用如下的 CREATE 语句,在 MySQL 数据库中创建一个名为dispatch的表 -
CREATE TABLE dispatches( ProductName VARCHAR(255), CustomerName VARCHAR(255), Price INT, Location VARCHAR(255), DispatchTimeStamp timestamp);
现在,我们将在 dispatches_data 表中插入 6 条记录,使用 INSERT 语句 -
insert into dispatches values('Key-Board', 'Raja', 7000, 'Hyderabad', TIMESTAMP('2019-05-04 15:02:45')); insert into dispatches values('Earphones', 'Roja', 2000, 'Vishakhapatnam', TIMESTAMP('2019-06-26 14:13:12')); insert into dispatches values('Mouse', 'Puja', 3000, 'Vijayawada', TIMESTAMP('2019-12-07 07:50:37')); insert into dispatches values('Mobile', 'Vanaja', 9000, 'Chennai', TIMESTAMP('2018-03-21 16:00:45')); insert into dispatches values('Headset', 'Jalaja', 6000, 'Goa', TIMESTAMP('2018-12-30 10:49:27')); insert into dispatches values('Watch', 'Rajan', 4000, 'Chennai', TIMESTAMP('2019-04-21 14:17:02'));
下面的 JDBC 程序会建立与数据库的连接,并重新排序 dispatches_data 表中的列。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ReorderingColumnsOfTable { public static void main(String args[])throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Query to re-order the table String query = "ALTER TABLE dispatches_data MODIFY DispatchTimeStamp timestamp AFTER CustomerName"; //Executing the query stmt.execute(query); //Retrieving the contents of the dispatches_data table stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from dispatches_data"); while(rs.next()) { System.out.print("Name: "+rs.getString("ProductName")+", "); System.out.print("Customer Name: "+rs.getString("CustomerName")+", "); System.out.print("Dispatch time stamp: "+rs.getTimestamp("DispatchTimeStamp")+", "); System.out.print("Price: "+rs.getInt("Price")+", "); System.out.print("Location: "+rs.getString("Location")); System.out.println(); } } }
在此程序中,我们使用 alter 命令将 DispatchTimeStamp 列从第 5 个位置移动到第 3 个位置。
输出
Connection established...... Name: Key-Board, Customer Name: Raja, Dispatch time stamp: 2019-05-04 15:02:45.0, Price: 7000, Location: Hyderabad Name: Earphones, Customer Name: Roja, Dispatch time stamp: 2019-06-26 14:13:12.0, Price: 2000, Location: Vishakhapatnam Name: Mouse, Customer Name: Puja, Dispatch time stamp: 2019-12-07 07:50:37.0, Price: 3000, Location: Vijayawada Name: Mobile, Customer Name: Vanaja, Dispatch time stamp: 2018-03-21 16:00:45.0, Price: 9000, Location: Chennai Name: Headset, Customer Name: Jalaja, Dispatch time stamp: 2018-12-30 10:49:27.0, Price: 6000, Location: Goa Name: Watch, Customer Name: Rajan, Dispatch time stamp: 2019-04-21 14:17:02.0, Price: 4000, Location: Chennai if you verify the contents of the table you can observe that the order of the columns has been changed.
mysql> select * from dispatches_data; +-------------+--------------+---------------------+-------+----------------+ | ProductName | CustomerName | DispatchTimeStamp | Price | Location | +-------------+--------------+---------------------+-------+----------------+ | Key-Board | Raja | 2019-05-04 15:02:45 | 7000 | Hyderabad | | Earphones | Roja | 2019-06-26 14:13:12 | 2000 | Vishakhapatnam | | Mouse | Puja | 2019-12-07 07:50:37 | 3000 | Vijayawada | | Mobile | Vanaja | 2018-03-21 16:00:45 | 9000 | Chennai | | Headset | Jalaja | 2018-12-30 10:49:27 | 6000 | Goa | | Watch | Rajan | 2019-04-21 14:17:02 | 4000 | Chennai | +-------------+--------------+---------------------+-------+----------------+ 6 rows in set (0.00 sec)
广告