我有一个应用程序,如果在Microsoft Access数据库中退出,我需要删除表。 我看到了代码here。我要删除的表名是 data_table ,访问数据库文件名是 local_entry 所以我需要更改代码,以便它适用于我的应用程序。
public void testDropTable () throws SQLException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MsAccessDSN");
Statement stmt = con.createStatement();
ResultSet checkTable = con.getMetaData().getTables(null, null, "POI", null);
String tableName = null;
while (checkTable.next())
{
System.out.println("In here");
tableName = checkTable.getString("TABLE_NAME");
System.out.println(tableName);
}
if (tableName != null){
try {
String dropTable = "DROP TABLE ";
String[] tables = DB_TABLE;
for (int i = 0; i < tables.length; i++){
String stringCode = new String();
stringCode = stringCode + tables[i];
System.out.println(dropTable + tables[i]);
// Drop each table in the array.
int temp = stmt.executeUpdate(dropTable + tables[i]);
}
}
catch (Exception e) {
System.err.println("Exception in testDropTable (): \n"
+ "Drop Table testDropTable threw an exception: " +(e.getMessage()));
}
}
else{
con.close();
}
}
我想我需要改变这两行:
String dropTable = "DROP TABLE ";
String[] tables = DB_TABLE;
我可以将 DROP TABLE 更改为 data_table 以及第二行。这是什么 DB_TABLE 我通过这种方式更改了整个代码,但直到出现问题:
public void testDropTable () throws SQLException{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
Connection con = DriverManager.getConnection("jdbc:odbc:MsAccessDSN");
Statement stmt = con.createStatement();
ResultSet checkTable = con.getMetaData().getTables(null, null, "POI", null);
String tableName = null;
while (checkTable.next())
{
System.out.println("In here");
tableName = checkTable.getString("data_table");
System.out.println(tableName);
}
if (tableName != null){
try {
String dropTable = "DROP TABLE ";
String[] tables = {"data_table"};
for (int i = 0; i < tables.length; i++){
String stringCode = new String();
stringCode = stringCode + tables[i];
System.out.println(dropTable + tables[i]);
// Drop each table in the array.
int temp = stmt.executeUpdate(dropTable + tables[i]);
}
}
catch (Exception e) {
System.err.println("Exception in testDropTable (): \n"
+ "Drop Table testDropTable threw an exception: " +(e.getMessage()));
}
}
else{
con.close();
}
}
答案 0 :(得分:1)
删除表格表名称; 是删除数据库中表格的命令。 尝试将 DB_TABLE 替换为 data_table 。
String dropTable = "DROP TABLE ";
String[] tables = data_table;
试试这个
public void testDropTable () throws SQLException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MsAccessDSN");
Statement stmt = con.createStatement();
ResultSet checkTable = con.getMetaData().getTables(null, null, "POI", null);
String tableName = null;
while (checkTable.next())
{
System.out.println("In here");
tableName = checkTable.getString("data_table");
System.out.println(tableName);
}
if (tableName != null){
try {
String dropTable = "DROP TABLE ";
String[] tables = tableName;
for (int i = 0; i < tables.length; i++){
String stringCode = new String();
stringCode = stringCode + tables[i];
System.out.println(dropTable + tables[i]);
// Drop each table in the array.
int temp = stmt.executeUpdate(dropTable + tables[i]);
}
}
catch (Exception e) {
System.err.println("Exception in testDropTable (): \n"
+ "Drop Table testDropTable threw an exception: " +(e.getMessage()));
}
}
else{
con.close();
}
}
答案 1 :(得分:1)
试试这段代码
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MsAccessDSN");
Statement stmt = con.createStatement();
// Specify the type of object; in this case we want tables
String[] types = {"TABLE"};
ResultSet checkTable = con.getMetaData().getTables(null, null, "%", types);
String tableName = null;
while (checkTable.next())
{
System.out.println("In here");
tableName = checkTable.getString(3)
System.out.println(tableName);
// check if the table 'data_table' exist in your database
if (tableName.equals("data_table"){
try {
//drop the table if present
int temp = stmt.executeUpdate("DROP TABLE " + tableName);
break;
}
catch (Exception e) {
System.err.println("Exception in testDropTable (): \n"
+ "Drop Table testDropTable threw an exception: " +(e.getMessage()));
}
}
}
con.close;
有关详细信息,请访问Metadata
答案 2 :(得分:0)
对于对此问题感兴趣的任何人,我都删除了已接受答案中的while循环语句,并将代码简化为:
public void testDropTable() throws SQLException, ClassNotFoundException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MsAccessDSN");
Statement stmt = con.createStatement();
String[] tables = {"data_table"};
for (String table : tables) {
try {
stmt.executeUpdate("DROP TABLE " + table);
} catch (SQLException e) {
System.err.println("Exception in testDropTable (): \n"
+ "Drop Table testDropTable threw an exception: " +(e.getMessage()));
}
}
con.close();
}