删除表(如果存在于Microsoft Access中) [英] delete table if exists in Microsoft Access

查看:76
本文介绍了删除表(如果存在于Microsoft Access中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,如果在Microsoft Access数据库中退出,我需要在其中删除表. 我在此处看到了代码.我要删除的表名是 data_table ,而访问数据库文件名是 local_entry 因此我需要更改代码以使其适用于我的应用程序.

I have one application in which I need to delete table if exit in Microsoft Access Database. I saw the code here. The table name which I want to delete is data_table and the access database file name is local_entry so where I need to change the code so it work for my application.

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();
    }
}

我想我需要更改这两行:

I think I need to change this two line:

String dropTable = "DROP TABLE ";
String[] tables = DB_TABLE;

我可以将 DROP TABLE (拖放表)更改为 data_table (数据表)吗?第二行呢?这是什么 DB_TABLE 我通过这种方式更改了整个代码,但是直到出现问题为止:

can I change DROP TABLE to data_table and what about second line. What is this DB_TABLE I change the whole code by this way but till problem is there:

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();
    }
}

推荐答案

尝试此代码

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; 

有关更多信息,请访问此处元数据

for more information visit here Metadata

这篇关于删除表(如果存在于Microsoft Access中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆