Apache Derby - Drop Table

DROP TABLE语句用于删除现有表,包括其所有触发器,约束和权限.

语法

以下是语法DROP TABLE语句.

 
 ij> DROP TABLE table_name;

示例

假设您在数据库中有一个名为Student的表.以下SQL语句删除名为Student的表.

ij> DROP TABLE Student;
0 rows inserted/updated/deleted

由于我们在尝试描述表时删除了表,我们将收到如下错误

ij> DESCRIBE Student;
IJ ERROR: No table exists with the name STUDENT

使用JDBC程序删除表的表

本节教您如何使用JDBC应用程序在Apache Derby数据库中删除表.

如果要使用网络客户端请求Derby网络服务器,请确保服务器已启动并正在运行.网络客户端驱动程序的类名是org.apache.derby.jdbc.ClientDriver,URL是jdbc:derby://localhost:1527 /DATABASE_NAME; create = true; user = USER_NAME ; passw
ord = PASSWORD "

按照以下步骤删除Apache Derby中的表格

步骤1:注册驱动程序

要与数据库通信,首先需要注册驱动程序. forName()的方法接受表示类名的String值将其加载到内存中,自动注册它.使用此方法注册驱动程序.

步骤2:获取连接

通常,我们与数据库进行通信的第一步是连接它.连接类代表物理连接与数据库服务器的连接.您可以通过调用 DriverManager 类的 getConnection()方法来创建连接对象.使用此我创建连接thod.

第3步:创建语句对象

您需要创建语句 PreparedStatement 或, CallableStatement 将SQL语句发送到数据库的对象.您可以分别使用方法 createStatement(),prepareStatement()和prepareCall()创建它们.使用适当的方法创建其中任何一个对象.

步骤4:执行查询

创建语句后,需要执行它. Statement 类提供了各种方法来执行查询,如 execute()方法,以执行返回多个结果集的语句. executeUpdate()方法执行INSERT,UPDATE,DELETE等查询. executeQuery()方法返回数据等结果.使用这些方法之一并执行先前创建的语句.

示例

以下JDBC示例演示了如何使用JDBC程序在Apache Derby中删除表.在这里,我们使用嵌入式驱动程序连接到名为sampleDB的数据库(如果它不存在,将创建它).

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DropTable {
   public static void main(String args[]) throws Exception {
      //Registering the driver
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

      //Getting the Connection object
      String URL = "jdbc:derby:sampleDB;create=true";
      Connection conn = DriverManager.getConnection(URL);

      //Creating the Statement object
      Statement stmt = conn.createStatement();

      //Executing the query
      String query = "DROP TABLE Employees";
      stmt.execute(query);
      System.out.println("Table dropped");
   }
}

输出

执行上述程序后,您将获得以下输出 :

Table dropped