2012-02-15 4 views
1

Microsoft Access 데이터베이스를 종료하면 테이블을 삭제해야하는 애플리케이션이 하나 있습니다. 코드 here을 보았습니다. 삭제할 테이블 이름은 이고 데이터 테이블은이고 액세스 데이터베이스 파일 이름은 입니다. local_entry 그래서 코드를 변경해야 응용 프로그램에서 작동합니다.Microsoft Access에 테이블이 존재하면 삭제

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

내가 DROP 표 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("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(); 
    } 
} 

는 나는이 두 라인을 변경할 필요가 있다고 생각합니다. 나는이 방법으로 전체 코드를 변경하지만 문제까지가이 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

여기에 자세한 내용은 방문 Metadata

+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

확인 chinna이 코드

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; 

을 시도하려고합니다. 이 코드에서 변경해야 할 내용은 다음과 같습니다. 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 인 문자열입니다 ... –

관련 문제