0
這是我第一次使用ucanaccess,因爲我剛發現我的以前版本的Eclipse不能用於Java 1.8,所以我可以訪問我的Access DB 。我熟悉Java編程和RDBMS(目前使用Oracle),所以我不明白爲什麼我的代碼出現錯誤。執行多行查詢不適用於單行查詢所在的表別名
這是我使用連接到DB中的碼(可變DBFILE是用來存儲訪問DB路徑的文件對象):
\t \t try
\t \t {
\t \t \t String urlDB = "jdbc:ucanaccess://"+ dbFile.getAbsolutePath();
\t \t \t
\t \t \t Connection conn = DriverManager.getConnection(urlDB, "", "");
\t \t \t System.out.println("Connection Successful\n");
\t \t \t
\t \t \t Statement test = conn.createStatement();
/* \t \t \t ResultSet rs = test.executeQuery("SELECT e.* "
+ "FROM Employees e"
+ "WHERE e.EmployeeNo LIKE 'H%'"
+ "ORDER BY e.LastName");
\t \t \t System.out.println(); //spacing
\t \t \t //retrieve column data
\t \t \t while(rs.next())
\t \t \t {
\t \t \t \t //can get data by column name instead of ID
\t \t \t \t String id = rs.getString("ID");
\t \t \t \t String fName = rs.getString("FirstName");
\t \t \t \t String lName = rs.getString("LastName");
\t \t \t \t String dName = rs.getString("DisplayName");
\t \t \t \t \t \t
\t \t \t \t System.out.println(id + "\t" + fName + "\t" + lName + "\t");
\t \t \t \t
\t \t \t }
*/ \t \t \t
\t \t \t ResultSet rs = test.executeQuery("SELECT e.* FROM Employees e WHERE e.EmployeeNo LIKE '1%' ORDER BY e.LastName");
\t \t \t
\t \t \t //get table information (i.e. column names)
\t \t \t ResultSetMetaData rsmd = rs.getMetaData();
\t \t \t int dbColumnCount = rsmd.getColumnCount();
\t \t \t
/* \t \t \t //db columns starts at 1 not 0
\t \t \t for (int count = 1; count <= dbColumnCount; count++)
\t \t \t {
\t \t \t \t System.out.print(rsmd.getColumnName(count) + "\t");
\t \t \t }
*/ \t \t \t
\t \t \t System.out.format("%5s%10s%15s%20s%n", rsmd.getColumnName(1), rsmd.getColumnName(2),
\t \t \t \t \t rsmd.getColumnName(3), rsmd.getColumnName(4));
\t \t \t //System.out.println();
\t \t \t
\t \t \t while(rs.next())
\t \t \t {
\t \t \t \t //can get data by column name instead of ID
\t \t \t \t String id = rs.getString("ID");
\t \t \t \t String fName = rs.getString("FirstName");
\t \t \t \t String lName = rs.getString("LastName");
\t \t \t \t String dName = rs.getString("DisplayName");
\t \t \t \t \t \t
\t \t \t \t //System.out.println(id + "\t" + fName + "\t" + lName + "\t");
\t \t \t \t System.out.format("%5s%10s%15s%20s%n", id, fName, lName, dName);
\t \t \t }
\t \t \t
\t \t }
\t \t catch (Exception e)
\t \t {
\t \t \t e.printStackTrace();
\t \t \t System.err.println("Could not connect to the Database");
\t \t \t System.err.println(e.getMessage());
\t \t } \t \t
\t }
到結果集的第一個呼叫(多行查詢)拋出了以下異常:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.0 user lacks privilege or object not found: E
\t at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:210)
\t at DBConnect.<init>(InOutBoard.java:78)
\t at InOutBoard.main(InOutBoard.java:47)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: E
\t at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
\t at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
\t at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
\t at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
\t at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:208)
\t ... 2 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: E
\t at org.hsqldb.error.Error.error(Unknown Source)
\t at org.hsqldb.error.Error.error(Unknown Source)
\t at org.hsqldb.QuerySpecification.resolveColumnReferencesForAsterisk(Unknown Source)
\t at org.hsqldb.QuerySpecification.resolveReferences(Unknown Source)
\t at org.hsqldb.QueryExpression.resolve(Unknown Source)
\t at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
\t at org.hsqldb.ParserCommand.compilePart(Unknown Source)
\t at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
\t at org.hsqldb.Session.executeDirectStatement(Unknown Source)
\t at org.hsqldb.Session.execute(Unknown Source)
\t ... 5 more
Could not connect to the Database
UCAExc:::4.0.0 user lacks privilege or object not found: E
而如果我運行第二次調用ResultSet(單行查詢),數據從表中正確返回。現在據我所知(除了在第一行中跨越多行),兩個查詢都是相同的。那爲什麼第一個拋出錯誤呢?我無法想象格式的選擇會有所作爲,但正如我所說,我知道使用ucanaccess。
任何幫助,將不勝感激。提前致謝!
哇 - 這就是爲什麼我應該離開程序並在稍後回來。對不起,謝謝你給我看這個錯誤。鏈接+1也非常有幫助 – Probius