如何访问 java Derby 数据库?我确实需要一些有用的建议
我是在 Eclipse 中使用 Derby 和数据库的新手,我有点迷失并且需要一些帮助。我已经建立了一个数据库连接,创建了一个新数据库和一个新架构,其中有一些包含一些测试数据的表。我对选择相关数据的 sql 查询没有任何问题。我遇到的问题是我已经可以使用查询了。我正在尝试创建一个连接到数据库的类,并出于测试目的,使用一个简单的查询来选择一些数据。这就是我到目前为止所拥有的:
public void getExerciseInfo() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connect = DriverManager.getConnection("jdbc:derby://localhost/c:/TestDatabase");
PreparedStatement statement = connect.prepareStatement("SELECT * from TESTSCHEMA.TESTTABLE");
resultSet = statement.executeQuery();
while (resultSet.next()) {
String name= resultSet.getString("NAME");
String type = resultSet.getString("TYPE");
System.out.println(name);
System.out.println(type);
}
} catch (Exception e) {
} finally {
close();
}
}
我想做的就是将表中的数据输出到控制台,但我什至无法完成这个简单的任务:(我猜我的连接网址无效,它应该是文件我的 Eclipse 工作区中数据库文件夹的路径?
无论如何,我很迷失,任何帮助将不胜感激。
I am new to using Derby and databses in eclipse, and I have become a tad lost and in need to a bit of help. I have established a database connection, created a new database, and a new schema, within which I have some tables containing some test data. I don't have any problem with the sql queries to select the relevant data. The problem I have is getting to a point where I can use queries. I am trying to create a class which connects to the database, and for testing purposes, uses a simple query to select some data. This is what I have so far:
public void getExerciseInfo() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connect = DriverManager.getConnection("jdbc:derby://localhost/c:/TestDatabase");
PreparedStatement statement = connect.prepareStatement("SELECT * from TESTSCHEMA.TESTTABLE");
resultSet = statement.executeQuery();
while (resultSet.next()) {
String name= resultSet.getString("NAME");
String type = resultSet.getString("TYPE");
System.out.println(name);
System.out.println(type);
}
} catch (Exception e) {
} finally {
close();
}
}
All I am trying to do is output the data in the table to the console, but I cant even do this simple task :( Im guessing my connection url is invalid, is it supposed to be the file path to the database folder in my eclipse workspace?
Anyhow, I am very lost, and any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否查看过: http://db.apache.org/derby/集成/plugin_help/derby_app.html?您似乎正在使用网络服务器,但您的数据库 URL 错误。
Did you take a look over: http://db.apache.org/derby/integrate/plugin_help/derby_app.html ? You seem to be using the network server but your db URL is wrong.
如果您没有运行 Derby 服务器,则可以建立一个 嵌入式数据库连接或使用
EmbeddedDataSource
,如图所示此处。If you are not running the Derby server, you can establish an embedded database connection or use an
EmbeddedDataSource
, shown here.