我使用h2(h2-1.4.186.jar)数据库作为Java应用程序。我创建了2个数据库,一个是我用于许多任务的支持数据库。 在其中一个任务中,我需要一个表(从表f中选择f.field,h.field,从另一个数据库加入表h ecc ...)。所以我使用了'创建链接表'。如果我从浏览器控制台执行查询它可以工作,但如果我在java方法中执行它不起作用。这是方法:
private void createTable()
{
String linkedTable="create force linked table if not exists "+TABLE_NAME+"('org.h2.Driver','jdbc:h2:../GLOBALDB/general','user','pass','("+getQuery()+")')";
try{
Connection conn=GenericDatabaseConnections.getConnectionDBThreads();
Statement sta=conn.createStatement();
try{
sta.execute(linkedTable);
sta.execute("SELECT * FROM "+TABLE_NAME);
}catch(Exception e){
e.printStackTrace();
}
sta.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
我得到以下例外情况:
org.h2.jdbc.JdbcSQLException: Error accessing linked table with SQL statement "SELECT * FROM (select * from film) T", cause: "org.h2.jdbc.JdbcSQLException: Table ""(select * from film)(org.h2.jdbc.JdbcSQLException: Table """"FILM"""" not found; SQL statement:
SELECT * FROM (select * from film) T WHERE 1=0 [42102-186])"" not found [42102-186]"; SQL statement:
SELECT * FROM film [90111-186]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.table.TableLink.wrapException(TableLink.java:462)
at org.h2.index.LinkedIndex.find(LinkedIndex.java:127)
at org.h2.index.BaseIndex.find(BaseIndex.java:127)
at org.h2.index.IndexCursor.find(IndexCursor.java:159)
at org.h2.table.TableFilter.next(TableFilter.java:329)
at org.h2.command.dml.Select.queryFlat(Select.java:574)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:686)
at org.h2.command.dml.Query.query(Query.java:322)
at org.h2.command.dml.Query.query(Query.java:290)
at org.h2.command.dml.Query.query(Query.java:36)
at org.h2.command.CommandContainer.query(CommandContainer.java:90)
at org.h2.command.Command.executeQuery(Command.java:197)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:179)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158)
at TableManager.createTable(TableManager.java:335)
at TableManager.task(TableManager.java:33)
at TaskManager.startTask(TaskManager.java:38)
at main.Main.main(Main.java:77)
Caused by: org.h2.jdbc.JdbcSQLException: Table "(select * from film)(org.h2.jdbc.JdbcSQLException: Table ""FILM"" not found; SQL statement:
SELECT * FROM (select * from film) T WHERE 1=0 [42102-186])" not found [42102-186]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.table.TableLink.readMetaData(TableLink.java:196)
at org.h2.table.TableLink.connect(TableLink.java:98)
at org.h2.table.TableLink.<init>(TableLink.java:78)
at org.h2.schema.Schema.createTableLink(Schema.java:607)
at org.h2.command.ddl.CreateLinkedTable.update(CreateLinkedTable.java:77)
at org.h2.engine.MetaRecord.execute(MetaRecord.java:58)
at org.h2.engine.Database.open(Database.java:732)
at org.h2.engine.Database.openDatabase(Database.java:266)
at org.h2.engine.Database.<init>(Database.java:260)
at org.h2.engine.Engine.openSession(Engine.java:60)
at org.h2.engine.Engine.openSession(Engine.java:167)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:145)
at org.h2.engine.Engine.createSession(Engine.java:128)
at org.h2.engine.Engine.createSession(Engine.java:26)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:347)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:108)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:92)
at org.h2.Driver.connect(Driver.java:72)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at databaseconnection.GenericDatabaseConnections.getConnectionDBThreads(GenericDatabaseConnections.java:46)
at TableManager.createTable(TableManager.java:329)
... 3 more
Caused by: org.h2.jdbc.JdbcSQLException: Table "FILM" not found; SQL statement:
SELECT * FROM (select * from film) T WHERE 1=0 [42102-186]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5251)
at org.h2.command.Parser.readTableFilter(Parser.java:1235)
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1874)
at org.h2.command.Parser.parseSelectSimple(Parser.java:1983)
at org.h2.command.Parser.parseSelectSub(Parser.java:1868)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1689)
at org.h2.command.Parser.readTableFilter(Parser.java:1159)
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1874)
at org.h2.command.Parser.parseSelectSimple(Parser.java:1983)
at org.h2.command.Parser.parseSelectSub(Parser.java:1868)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1689)
at org.h2.command.Parser.parseSelect(Parser.java:1677)
at org.h2.command.Parser.parsePrepared(Parser.java:433)
at org.h2.command.Parser.parse(Parser.java:305)
at org.h2.command.Parser.parse(Parser.java:277)
at org.h2.command.Parser.prepareCommand(Parser.java:242)
at org.h2.engine.Session.prepareLocal(Session.java:446)
at org.h2.engine.Session.prepareCommand(Session.java:388)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1189)
at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:73)
at org.h2.table.TableLink.readMetaData(TableLink.java:173)
... 24 more
最后如果我在浏览器控制台打开的时候在java中这样做,它就可以了。 我该怎么办呢? 附:抱歉我的英文不好