2012-02-15 10 views
1

Microsoft Accessデータベースを終了する場合は、テーブルを削除する必要があるアプリケーションが1つあります。 コードhereを見ました。削除したいテーブル名はdata_tableであり、アクセスデータベースファイル名はlocal_entry ですので、アプリケーションのためにコードを変更する必要があります。Microsoft Accessにテーブルが存在する場合は

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

は、私は2番目のラインについて data_table、何のDROP 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("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(); 
    } 
} 

私は、私はこの2行を変更する必要があると思います。私はこの方法で全体のコードを変更しますが、問題まであり、この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(); 
    } 
} 

答えて

1
+0

おかげで... まさにこのコードの仕事。 あなたの質問が好きなら、私の質問をアップグレードしてください。 –

+0

@shrishなぜOPの質問からそのコードブロック全体を削除しましたか? vinitvikashは不要なコードブロックですか? – gideon

+0

@gideon OPが正しいかどうかを確認するために変更されたコードの行だけです。私はこの質問が自明だと思います。 – Shirish11

1

ドロップテーブルのテーブル名;はデータベースにテーブルをドロップするコマンドです。 DB_TABLEdata_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(); 
    } 
} 
+0

に、ここでより多くの情報訪問のため

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; 

OK chinnaをこのコードを試してみてください、実際に私はいくつかのミスを犯しました。 このコードで変更する必要があるもの: tableName = checkTable.getString( "TABLE_NAME"); ここで私はdata_tableでTABLE_NAMEを変更する必要がありますか? あなたは、DB_TABLEをdata_tableで変更すると言っています。しかし、テーブルは文字列の集合です。 –

+0

私はこの方法でコード全体を書きますが、それが私のアプリケーションでは機能しなくなるまで: –

+0

hi vinitvikash。 DB_Tableをtable_nameまたはdata_tableに置き換えることができます。 Table_nameは実際に値がdata_table ...である文字列です。 –

関連する問題