2011-12-17 39 views
0

在運行下面的代碼自定義數據庫表設計使用Java和MS Access

public class Temp { 

    public static void main(String args[]) { 

     Connection con; // The connection to the database. 
     // The following code can throw errors, so they must be caught. 
     try{ 

      // First, tell Java what driver to use and where to find it. 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      // Next, create a connection to your data source. 
      // Specify that you are using the ODBC-JDBC Bridge. 
      // And specify the data source from ODBC. 
      con = DriverManager.getConnection("jdbc:odbc:Temp"); 
      // Create an SQL statement. 
      Statement stmt = con.createStatement(); 
      // Execute some SQL to create a table in your database. 
      // If the table already exists, an exception is thrown! 
      stmt.executeUpdate("CREATE TABLE COFFEES " + 
      "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " + 
      "SALES INTEGER, TOTAL INTEGER)"); 

     } 
     // Catch any exceptions that are thrown. 
     catch(ClassNotFoundException e){ 

      System.out.println(e.toString()); 

     } 
     catch(SQLException e){ 

      System.out.println(e.toString()); 

     } 

    } 

} 

我得到了錯誤的

值java.sql.SQLException:[微軟] [ODBC Microsoft Access驅動程序]不能修改'COFFEES'表的設計。它位於只讀數據庫中。

請幫助

回答

0

嘗試刪除該表明確並再次運行。

+0

我沒有創建數據庫 – user1234 2011-12-17 06:24:02

+0

我有比OP略有不同的說法。我的聲明是「DROP TABLE MyTest」。 Access未安裝在服務器上,因此打開GUI不是一個選項。 – 2016-06-02 20:18:22

2

確保您有當前用戶對數據庫/文件的寫入權限。

+0

我沒有創建數據庫 – user1234 2011-12-17 06:23:47

+0

做一個右鍵單擊它,並確保您有寫入權限的文件。 – 2011-12-17 06:24:50

1

您需要添加"ReadOnly=False;"到連接字符串

1

檢查在ODBC DSN中的高級選項,並確保ReadOnly設置爲0

enter image description here