- Java 编程示例
- 示例 - 主页
- 示例 - 环境
- 示例 - 字符串
- 示例 - 数组
- 示例 - 日期和时间
- 示例 - 方法
- 示例 - 文件
- 示例 - 目录
- 示例 - 异常
- 示例 - 数据结构
- 示例 - 集合
- 示例 - 网络
- 示例 - 线程
- 示例 - 小应用程序
- 示例 - 简单图形用户界面
- 示例 - JDBC
- 示例 - 正则表达式
- 示例 - Apache PDF Box
- 示例 - Apache POI PPT
- 示例 - Apache POI Excel
- 示例 - Apache POI Word
- 示例 - OpenCV
- 示例 - Apache Tika
- 示例 - iText
- Java 教程
- Java - 教程
- Java 实用资源
- Java - 快速指南
- Java - 实用资源
如何编辑(添加或更新)表中的列,以及如何删除 Java 中的表
问题说明
如何编辑(添加或更新)表中的列,以及如何删除表?
解决方案
以下示例使用 create、alter 和 drop SQL 命令来创建、编辑或删除表。
import java.sql.*;
public class jdbcConn {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection(
"jdbc:derby://:1527/testDb","username", "password");
Statement stmt = con.createStatement();
String query ="CREATE TABLE employees(id INTEGER PRIMARY KEY, first_name CHAR(50), last_name CHAR(75))";
stmt.execute(query);
System.out.println("Employee table created");
String query1 = "aLTER TABLE employees ADD address CHAR(100) ";
String query2 = "ALTER TABLE employees DROP COLUMN last_name";
stmt.execute(query1);
stmt.execute(query2);
System.out.println("Address column added to the table & last_name column removed from the table");
String query3 = "drop table employees";
stmt.execute(query3);
System.out.println("Employees table removed");
}
}
结果
上面代码示例将产生以下结果。结果可能有所不同。
Employee table created Address column added to the table & last_name column removed from the table Employees table removed from the database
java_jdbc.htm
广告