H2:所有其他计算机上均出现错误
我在 Java 项目中使用 H2 数据库(嵌入模式)。 在我家里的计算机上一切正常,可以建立连接,但在所有其他计算机上我总是收到以下错误:
org.h2.jdbc.JdbcSQLException:找不到表“CUSTOMERS”; SQL 语句:SELECT * FROM CUSTOMERS [42102-162]
我确信,在数据库中一切都很好,它应该与连接有关。 但即使我导入 h2-1.3.162.jar 文件,错误仍然存在。
String dbClass = "org.h2.Driver";
String dbDriver = "jdbc:h2:~/cc";
String user = "user1";
String pass = "test1";
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public void connect() {
boolean done = false;
//load driver
try {
Class.forName(dbClass).newInstance();
System.out.println("driver loaded"); // This is shown in the Compiler
} catch (Exception ex) {
System.out.println("error while loading driver");
System.err.println(ex);
}
// Connection
try {
conn = DriverManager.getConnection(dbDriver, user, pass);
System.out.println("connected"); // This is shown in the Compiler
done = true;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public Vector select() {
data = new Vector();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM CUSTOMERS");
while (rs.next()) {
Vector row = new Vector();
row.add(rs.getInt("id"));
row.add(rs.getString("fname"));
row.add(rs.getString("lname"));
row.add(rs.getString("street"));
row.add(rs.getString("city"));
row.add(rs.getString("zip"));
row.add(rs.getString("state"));
row.add(rs.getString("phone"));
row.add(rs.getString("birthday"));
row.add(rs.getString("email"));
row.add(rs.getInt("code"));
data.add(row);
}
rs.close();
stmt.close();
} catch (SQLException ex) {
System.out.println("error while selecting"); // I receive this error
System.err.println(ex);
}
return data;
}
I'm using the H2 database in my Java project (embedded mode).
On my computer at home everything works, the connection can be established, but on all other computers I always receive the following error:
org.h2.jdbc.JdbcSQLException: Table "CUSTOMERS" not found; SQL
statement: SELECT * FROM CUSTOMERS [42102-162]
I'm sure, that within the DB everything is alright, it should be something with the connection.
But even if I import the h2-1.3.162.jar file, the error still remains.
String dbClass = "org.h2.Driver";
String dbDriver = "jdbc:h2:~/cc";
String user = "user1";
String pass = "test1";
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public void connect() {
boolean done = false;
//load driver
try {
Class.forName(dbClass).newInstance();
System.out.println("driver loaded"); // This is shown in the Compiler
} catch (Exception ex) {
System.out.println("error while loading driver");
System.err.println(ex);
}
// Connection
try {
conn = DriverManager.getConnection(dbDriver, user, pass);
System.out.println("connected"); // This is shown in the Compiler
done = true;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public Vector select() {
data = new Vector();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM CUSTOMERS");
while (rs.next()) {
Vector row = new Vector();
row.add(rs.getInt("id"));
row.add(rs.getString("fname"));
row.add(rs.getString("lname"));
row.add(rs.getString("street"));
row.add(rs.getString("city"));
row.add(rs.getString("zip"));
row.add(rs.getString("state"));
row.add(rs.getString("phone"));
row.add(rs.getString("birthday"));
row.add(rs.getString("email"));
row.add(rs.getInt("code"));
data.add(row);
}
rs.close();
stmt.close();
} catch (SQLException ex) {
System.out.println("error while selecting"); // I receive this error
System.err.println(ex);
}
return data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题不在于您的连接,因为如果无法连接到数据库,您会在此之前收到异常。异常也很清楚地说明了问题是什么 - 它找不到
CUSTOMERS
表。这可能是因为该表根本不存在,或者连接指向错误的数据库;尝试输入表的完整架构信息,而不仅仅是其名称,然后看看是否有效。The problem isn't with your connection as you'd receive an exception well before then if it was failing to connect to the database. The exception is pretty clear about what the issue is, as well - it can't find the
CUSTOMERS
table. That could be because the table doesn't exist at all, or the connection is pointing at the wrong database; try putting in the full schema information of the table, rather than just its name, and see if that works.检查你的假设。这是不正确的。
消息中没有任何内容表明您无法连接。要么您连接到了错误的数据库,要么您连接到的数据库没有创建表客户。 (应该命名为 CUSTOMER,而不是复数。)
如果您不再假设您所做的一切都是正确的,您将更快地修复错误。你应该假设一切都是错的。
当您捕获该异常时,我会打印堆栈跟踪。它会给你更多的信息。
Check your assumptions. This one is incorrect.
There's nothing in the message to suggest that you couldn't connect. Either you connected to the wrong database OR the one you did connect to didn't CREATE TABLE CUSTOMERS. (Should be named CUSTOMER, not plural.)
You'll fix your error faster if you stop assuming that everything you did is correct. You should be assuming that everything is wrong.
I'd print the stack trace when you catch that exception. It'll give you more information.
终于明白了!
它与我的表无关,找不到数据库。当尝试连接到无法通过
String dbDriver = "jdbc:h2:~/cc";
找到的数据库时,名称为 cc 的新数据库 (在我的例子中)将被创建(当然是一个没有表的空表)并建立连接。这就是为什么我没有收到任何连接错误。在下一步中,我尝试从新创建的空数据库中检索一些数据,因此收到错误,我的表不存在。
因此我更改了这一行:
String dbDriver =“jdbc:h2: file:lib/cc";
并将旧数据库 cc.h2.db 复制到应用程序的 lib 目录中。就这样!< br>
PS:这是一个类似的问题:h2(嵌入模式)数据库文件问题
Finally I figured it out!
It had nothing to do with my tables, the database couldn't be found. When trying to connect to a database which can't be found with
String dbDriver = "jdbc:h2:~/cc";
, a new database with the name cc (in my case) will be created (of course an empty one with no tables) and the connection is established. That's why I haven't received any connection errors.In the next step I tried to retrieve some data from the new created empty database and therefore received the error, that my table doesn't exist.
So I changed this line:
String dbDriver = "jdbc:h2:file:lib/cc";
and copied into the lib directory of my application my old database cc.h2.db.That's all!
PS: Here is a similiar problem: h2 (embedded mode ) database files problem