2011-10-20 74 views
1

我使用開放式辦公數據庫3.3作爲我的數據庫。我能夠成功建立連接,但在試圖執行查詢得到一個錯誤:開放式辦公數據庫查詢

public class openofficeupdate { 
String databaseurl="C:\\Users\\RAVITEJA\\Documents\\BluetoothExchangeFolder\\salesforce.odb"; 
openofficeupdate() throws ClassNotFoundException, SQLException{ 
    System.out.println("Entered into constructor"); 
    Connection connection=null; 
    Statement statement=null; 
    try{ 
    Class c=openofficeclass(); 
    System.out.println("Class name set"); 

    Connection cntn=createConnection(databaseurl); 
    connection=cntn; 
    System.out.println("connection created"); 

    Statement stmt=createStatement(cntn); 
    statement=stmt; 
    System.out.println("Statement created"); 

    executeQueries(stmt); 
    System.out.println("Query executed"); 

    closeStatement(stmt); 
    System.out.println("Statement closed"); 

    closeConnection(cntn); 
    System.out.println("Connection closed"); 

    }catch(Exception e){ 
     System.out.println(e); 

     closeStatement(statement); 
     System.out.println("Statement closed"); 

     closeConnection(connection); 
     System.out.println("Connection closed"); 
    } 
} 
public static void main(String args[]) throws ClassNotFoundException, SQLException{ 
    new openofficeupdate(); 
} 

private Class openofficeclass() throws ClassNotFoundException { 
    return Class.forName("org.hsqldb.jdbcDriver"); 
} 

private Connection createConnection(String databaseurl) throws SQLException{ 
    return DriverManager.getConnection("jdbc:hsqldb:file:" +databaseurl); 
} 

private Statement createStatement(Connection cntn) throws SQLException{ 
    return cntn.createStatement(); 
} 

private void closeStatement(Statement stmt) throws SQLException{ 
    stmt.close(); 
} 

private void closeConnection(Connection cntn) throws SQLException{ 
    cntn.close(); 
} 

private void executeQueries(Statement stmt) throws SQLException{ 

    System.out.println("Going to execute query"); 
    //int status=stmt.executeUpdate("insert into Mobiles(Mobile ID,Employee ID,Start_Track_Time,Stop_Track_Time) values(987654321,198,09:30:00,10:30:00)"); 
    ResultSet rs=stmt.executeQuery("select * from Mobiles;"); 
    while(rs.next()){ 
     System.out.println("Inside row "+rs.getRowId(1)); 
    } 
    System.out.println("Query executed with status "); 
} 
} 

我設置的hsqldb.jar我的類路徑。上面顯示的代碼的輸出是...

Entered into constructor 
Class name set 
connection created 
Statement created 
Going to execute query 
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: MOBILES 
Statement closed 
Connection closed 

這是怎麼回事?

回答

0

當您連接到數據庫時,您將以沒有權限訪問數據庫中的某個實體的用戶身份執行此操作。您需要將用戶設置爲與具有相應權限的用戶的連接。根據這個你想要的用戶名是SA

數據庫開發人員可能會驚訝地發現OpenOffice.org Base已經有一個用戶帳戶。此用戶帳戶(名爲SA)...

使用連接對象上的setClientInfo()方法將用戶名設置爲「SA」。

+0

謝謝@Kurtis Nusbaum,但我怎樣才能在開放式辦公數據庫中爲用戶設置適當的權限。 –

+0

你從Connection類獲得什麼包?這將幫助我確定如何更好地回答你。 –

+0

我正在使用java.sql。*;爲sql連接和查詢 –