答:SQL DROP TABLE语句用于删除表定义以及该表的所有数据,索引,触发器,约束和权限规范。
DROP TABLE table_name;
要使用JDBC API从数据库中删除表,您需要:
注册驱动程序:使用DriverManager 类的 registerDriver()方法注册驱动程序类。将驱动程序类名称作为参数传递给它。
建立连接:使用DriverManager 类的getConnection()方法连接数据库。将URL(字符串),用户名(字符串),密码(字符串)作为参数传递给它。
Create Statement:使用Connection 接口的createStatement()方法创建一个Statement对象。
执行查询:使用Statement接口的execute()方法执行查询。
show tables命令为您提供MySQL当前数据库中的表列表。首先,使用以下命令验证名为mydatabase的数据库中的表列表:
show tables; +----------------------+ | Tables_in_mydatabase | +----------------------+ | customers | +----------------------+ 1 row in set (0.02 sec)
以下JDBC程序建立与MySQL的连接,并从名为mydatabase的数据库中删除名为customers的表:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DropTableExample {
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://localhost/ExampleDatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Query to drop a table
String query = "Drop table Customers";
//Executing the query
stmt.execute(query);
}
}输出结果
Connection established...... Table Dropped......