我正在尝试使用H2数据库( v1.3.170 )在我的项目中进行JUnit测试,并遇到一种奇怪的行为。 我有一个包含所有sqls的jar用于数据库模式初始化:
db_data.jar
|--schema
|--aliases.sql
|--tables.sql
|-- ...
我的classpath中有这个jar。但是当我执行下面的代码时 - 它会引发异常[见下文]:
CallableStatement initCall = conn.prepareCall("RUNSCRIPT FROM 'classpath:schema/aliases.sql'");
initCall.execute();
所见例外:
Caused by: org.h2.jdbc.JdbcSQLException: IO Exception: "java.io.FileNotFoundException: resource /schema/aliases.sql"; "classpath:schema/aliases.sql"; SQL statement:
RUNSCRIPT FROM 'classpath:schema/aliases.sql' [90031-170]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:158)
at org.h2.message.DbException.convertIOException(DbException.java:315)
at org.h2.command.dml.ScriptBase.openInput(ScriptBase.java:162)
at org.h2.command.dml.RunScriptCommand.update(RunScriptCommand.java:43)
at org.h2.command.CommandContainer.update(CommandContainer.java:75)
at org.h2.command.Command.executeUpdate(Command.java:230)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:333)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:149)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: resource /schema/aliases.sql
at org.h2.store.fs.FilePathDisk.newInputStream(FilePathDisk.java:285)
at org.h2.store.fs.FileUtils.newInputStream(FileUtils.java:209)
at org.h2.command.dml.ScriptBase.openInput(ScriptBase.java:160)
... 6 more
我在org.h2.store.fs.FilePathDisk#newInputStream()
中看到你明确地将/
添加到SQL文件的路径中:
public InputStream newInputStream() throws IOException {
//...
if (!fileName.startsWith("/")) {
fileName = "/" + fileName;
}
//...
return in;
}
您能解释一下上述目的吗?以及如何处理异常?
提前致谢!
答案 0 :(得分:1)
使用服务器模式时,资源(jar文件)需要位于服务器端。
答案 1 :(得分:-1)
尝试将文件引用更改为:
classpath*:schema/aliases.sql