执行多行查询不适用于 table 别名,而单行查询可以
Executing multiline query does not work with table alias where single-line query does
这是我第一次使用 ucanaccess,因为我刚刚发现我以前的 Eclipse 版本不能与 Java 1.8 一起使用,所以我可以访问我的 Access 数据库。我熟悉 Java 编程和 RDBMS(目前使用 Oracle),所以我不明白为什么我的代码会出错。
这是我用来连接DB的代码(变量dbFile是一个File对象,用来存储Access DB路径):
try
{
String urlDB = "jdbc:ucanaccess://"+ dbFile.getAbsolutePath();
Connection conn = DriverManager.getConnection(urlDB, "", "");
System.out.println("Connection Successful\n");
Statement test = conn.createStatement();
/* ResultSet rs = test.executeQuery("SELECT e.* "
+ "FROM Employees e"
+ "WHERE e.EmployeeNo LIKE 'H%'"
+ "ORDER BY e.LastName");
System.out.println(); //spacing
//retrieve column data
while(rs.next())
{
//can get data by column name instead of ID
String id = rs.getString("ID");
String fName = rs.getString("FirstName");
String lName = rs.getString("LastName");
String dName = rs.getString("DisplayName");
System.out.println(id + "\t" + fName + "\t" + lName + "\t");
}
*/
ResultSet rs = test.executeQuery("SELECT e.* FROM Employees e WHERE e.EmployeeNo LIKE '1%' ORDER BY e.LastName");
//get table information (i.e. column names)
ResultSetMetaData rsmd = rs.getMetaData();
int dbColumnCount = rsmd.getColumnCount();
/* //db columns starts at 1 not 0
for (int count = 1; count <= dbColumnCount; count++)
{
System.out.print(rsmd.getColumnName(count) + "\t");
}
*/
System.out.format("%5s%10s%15s%20s%n", rsmd.getColumnName(1), rsmd.getColumnName(2),
rsmd.getColumnName(3), rsmd.getColumnName(4));
//System.out.println();
while(rs.next())
{
//can get data by column name instead of ID
String id = rs.getString("ID");
String fName = rs.getString("FirstName");
String lName = rs.getString("LastName");
String dName = rs.getString("DisplayName");
//System.out.println(id + "\t" + fName + "\t" + lName + "\t");
System.out.format("%5s%10s%15s%20s%n", id, fName, lName, dName);
}
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("Could not connect to the Database");
System.err.println(e.getMessage());
}
}
第一次调用 ResultSet(多行查询)抛出以下异常:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.0 user lacks privilege or object not found: E
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:210)
at DBConnect.<init>(InOutBoard.java:78)
at InOutBoard.main(InOutBoard.java:47)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: E
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:208)
... 2 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: E
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.QuerySpecification.resolveColumnReferencesForAsterisk(Unknown Source)
at org.hsqldb.QuerySpecification.resolveReferences(Unknown Source)
at org.hsqldb.QueryExpression.resolve(Unknown Source)
at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 5 more
Could not connect to the Database
UCAExc:::4.0.0 user lacks privilege or object not found: E
而如果我 运行 第二次调用 ResultSet(单行查询),则数据会从 table 正确返回。现在据我所知(除了在第一行中跨越多行)这两个查询是相同的。那么为什么第一个抛出错误呢?我无法想象格式的选择会有所不同,但正如我所说,我知道使用 ucanaccess。
如有任何帮助,我们将不胜感激。提前致谢!
我经常给出的 link 在这里也有效:
How to debug dynamic SQL in VBA
您正在连接一个 SQL 字符串而没有检查它是否正确。
ResultSet rs = test.executeQuery("SELECT e.* "
+ "FROM Employees e"
+ "WHERE e.EmployeeNo LIKE 'H%'"
+ "ORDER BY e.LastName");
将导致
SELECT e.* FROM Employees eWHERE e.EmployeeNo LIKE 'H%'ORDER BY e.LastName
换行符之间缺少空格。
这是我第一次使用 ucanaccess,因为我刚刚发现我以前的 Eclipse 版本不能与 Java 1.8 一起使用,所以我可以访问我的 Access 数据库。我熟悉 Java 编程和 RDBMS(目前使用 Oracle),所以我不明白为什么我的代码会出错。
这是我用来连接DB的代码(变量dbFile是一个File对象,用来存储Access DB路径):
try
{
String urlDB = "jdbc:ucanaccess://"+ dbFile.getAbsolutePath();
Connection conn = DriverManager.getConnection(urlDB, "", "");
System.out.println("Connection Successful\n");
Statement test = conn.createStatement();
/* ResultSet rs = test.executeQuery("SELECT e.* "
+ "FROM Employees e"
+ "WHERE e.EmployeeNo LIKE 'H%'"
+ "ORDER BY e.LastName");
System.out.println(); //spacing
//retrieve column data
while(rs.next())
{
//can get data by column name instead of ID
String id = rs.getString("ID");
String fName = rs.getString("FirstName");
String lName = rs.getString("LastName");
String dName = rs.getString("DisplayName");
System.out.println(id + "\t" + fName + "\t" + lName + "\t");
}
*/
ResultSet rs = test.executeQuery("SELECT e.* FROM Employees e WHERE e.EmployeeNo LIKE '1%' ORDER BY e.LastName");
//get table information (i.e. column names)
ResultSetMetaData rsmd = rs.getMetaData();
int dbColumnCount = rsmd.getColumnCount();
/* //db columns starts at 1 not 0
for (int count = 1; count <= dbColumnCount; count++)
{
System.out.print(rsmd.getColumnName(count) + "\t");
}
*/
System.out.format("%5s%10s%15s%20s%n", rsmd.getColumnName(1), rsmd.getColumnName(2),
rsmd.getColumnName(3), rsmd.getColumnName(4));
//System.out.println();
while(rs.next())
{
//can get data by column name instead of ID
String id = rs.getString("ID");
String fName = rs.getString("FirstName");
String lName = rs.getString("LastName");
String dName = rs.getString("DisplayName");
//System.out.println(id + "\t" + fName + "\t" + lName + "\t");
System.out.format("%5s%10s%15s%20s%n", id, fName, lName, dName);
}
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("Could not connect to the Database");
System.err.println(e.getMessage());
}
}
第一次调用 ResultSet(多行查询)抛出以下异常:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.0 user lacks privilege or object not found: E
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:210)
at DBConnect.<init>(InOutBoard.java:78)
at InOutBoard.main(InOutBoard.java:47)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: E
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:208)
... 2 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: E
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.QuerySpecification.resolveColumnReferencesForAsterisk(Unknown Source)
at org.hsqldb.QuerySpecification.resolveReferences(Unknown Source)
at org.hsqldb.QueryExpression.resolve(Unknown Source)
at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 5 more
Could not connect to the Database
UCAExc:::4.0.0 user lacks privilege or object not found: E
而如果我 运行 第二次调用 ResultSet(单行查询),则数据会从 table 正确返回。现在据我所知(除了在第一行中跨越多行)这两个查询是相同的。那么为什么第一个抛出错误呢?我无法想象格式的选择会有所不同,但正如我所说,我知道使用 ucanaccess。
如有任何帮助,我们将不胜感激。提前致谢!
我经常给出的 link 在这里也有效:
How to debug dynamic SQL in VBA
您正在连接一个 SQL 字符串而没有检查它是否正确。
ResultSet rs = test.executeQuery("SELECT e.* "
+ "FROM Employees e"
+ "WHERE e.EmployeeNo LIKE 'H%'"
+ "ORDER BY e.LastName");
将导致
SELECT e.* FROM Employees eWHERE e.EmployeeNo LIKE 'H%'ORDER BY e.LastName
换行符之间缺少空格。