您可以使用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");
//将Time对象转换为String格式
String time = timeObj.toString();让我们使用CREATE语句在MySQL数据库中创建一个带有名称调度的表,如下所示:
CREATE TABLE dispatches( ProductName VARCHAR(255), CustomerName VARCHAR(255), Price INT, Location VARCHAR(255), DispatchTimeStamp timestamp);
现在,我们将使用INSERT语句在dispatches_data表中插入6条记录-
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 {
//注册驱动程序
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对象
Statement stmt = con.createStatement();
//查询以重新排序表格
String query = "ALTER TABLE dispatches_data MODIFY DispatchTimeStamp timestamp AFTER CustomerName";
//执行查询
stmt.execute(query);
//检索dispatches_data表的内容
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)