我使用开放式办公室数据库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 :(得分:0)
当您连接到数据库时,您正在以无权访问数据库中某个实体的用户身份进行操作。您需要为具有相应权限的用户设置连接的用户。根据这个你想要的用户名是SA
数据库开发人员可能会惊讶地发现OpenOffice.org Base已经拥有一个用户帐户。此用户帐户(名为SA)...
使用Connection对象上的setClientInfo()方法将用户名设置为“SA”。