在java中使用此代码:
Connection c = jdbcDriver.getConnection("jdbc:hsqldb:mem:test;sql.ignore_case=true", null);
Statement stmt = c.createStatement();
stmt.executeUpdate("CREATE TABLE \"TEST\"(ID INT)");
stmt.executeQuery("SELECT id FROM \"test\";");
抛出org.hsqldb.HsqlException
Exception in thread "main" java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: test
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 Test.main(Test.java:15)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: test
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readTableOrSubquery(Unknown Source)
at org.hsqldb.ParserDQL.XreadTableReference(Unknown Source)
at org.hsqldb.ParserDQL.XreadFromClause(Unknown Source)
at org.hsqldb.ParserDQL.XreadTableExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryExpression(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)
... 3 more
但为什么呢?我说ignore_case=true
!
请参阅此链接:http://hsqldb.org/doc/guide/dbproperties-chapt.html#N15A0A
答案 0 :(得分:3)
默认情况下,如果没有引号,名称不区分大小写。但是您将表名放在引号之间,因此它们完全按照区分大小写进行注册。这两个表名不同,HSQLDB也不匹配。
这将有效:
import java.sql.*;
public class Main {
public static void main(String[] args) throws Exception {
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:test", "SA", "");
Statement stmt = c.createStatement();
stmt.executeUpdate("CREATE TABLE TEST(ID INT)");
stmt.executeQuery("SELECT id FROM test;");
}
}
sql.ignore_case
做什么呢?确保 varchar 比较不区分大小写。例如,abc
将与ABC
匹配:
import java.sql.*;
public class Main {
public static void main(String[] args) throws Exception {
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:test;sql.ignore_case=true", "SA", "");
Statement stmt = c.createStatement();
stmt.executeUpdate("CREATE TABLE TeSt(NAME VARCHAR(99))");
stmt.executeUpdate("INSERT INTO tEsT (NAME) VALUES ('ABC')");
ResultSet rs = stmt.executeQuery("SELECT * FROM TesT WHERE NAME = 'abc'");
rs.next();
System.out.println(rs.getString(1)); // prints: ABC
}
}