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