【发布时间】:2014-03-22 12:24:48
【问题描述】:
您好,我用一个简单的 java 应用程序创建了一个 derby 嵌入式数据库。 当在 Eclipse 上测试运行时,它运行完美。然后我导出为可运行的 jar 文件。通过 cmd 运行 找不到异常数据库..!!!
public class Main {
public static void main(String[] args) throws SQLException {
final String driver="org.apache.derby.jdbc.EmbeddedDriver";
final String url="jdbc:derby:db/testdb";
try {
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
//connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20))");
// connection.createStatement().execute("insert into channels (channel,topic) values('hbo','action')");
// System.out.println("saved");
PreparedStatement preStmt=connection.prepareStatement("select * from channels");
ResultSet set=null;
set=preStmt.executeQuery();
while(set.next()){
System.out.print(set.getString(1));
System.out.println(set.getString(2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
错误
Exception in thread "main" SQL Exception: Database 'db/testdb' not found.
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Sourc
我的需要是当我在其他启用 java 的电脑上运行 jar 文件时它必须运行..!!! 我已经在其他电脑上尝试过它给了我同样的错误..! 我怎样才能创建数据库.... !! 有人知道请帮忙..!
【问题讨论】:
-
创建应用程序启动的目录
db... -
我将 url 更改为 create=true ,然后我运行它,jar 自动创建了 db 目录..现在它显示 table not found...
-
我怎样才能让数据库和表只创建一次..!!
-
您可以检查数据库元数据以检查表是否存在或查看this question