【发布时间】:2018-09-10 08:08:05
【问题描述】:
我正在创建一个 JavaFX 独立应用程序并使用嵌入式 derby 数据库并通过 JDBC 连接到它。问题是每当我尝试与数据库表交互时,我都会收到“java.sql.SQLSyntaxErrorException:表/视图'BUILDINGS'不存在。”。
我已经搜索了很多,但我无法解决它。我已经查找了提到here 的三个原因,但我仍然遇到同样的错误。 This 的帖子听起来与我的情况非常相似,但我不知道他是如何解决的。我已经从连接元数据和 netbeans 服务选项卡中检查了数据库 URL,它是相同的。
这里是与问题相关的代码sn-ps:
public class DataBaseUtility {
public Connection derbyDBConnection(String url){
Connection con = null;
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url, "", "");
con.setSchema("APP");
} catch (ClassNotFoundException | SQLException |
InstantiationException | IllegalAccessException ex) {
Logger.getLogger(DataBaseUtility.class.getName())
.log(Level.SEVERE, null, ex);
}
return con;
}
public ResultSet databaseQuerying(Connection con, String query){
ResultSet resultSet = null;
try {
PreparedStatement preStmt = con.prepareStatement(query);
resultSet = preStmt.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(DataBaseUtility.class.getName()).log(Level.SEVERE, null, ex);
}
return resultSet;
}}
和
public class MainApp extends Application {
public static DataBaseUtility dbUtility ;
public static Connection con;
@Override
public void init(){
dbUtility = new DataBaseUtility();
con = dbUtility.derbyDBConnection("jdbc:derby:elsarhEmbedded");
try {
DatabaseMetaData dbmd = con.getMetaData();
System.out.println("\n----------------------------------------------------") ;
System.out.println("Database Name = " + dbmd.getDatabaseProductName()) ;
System.out.println("Database User Name= " + dbmd.getUserName()) ;
System.out.println("Database Version = " + dbmd.getDatabaseProductVersion()) ;
System.out.println("Driver Name = " + dbmd.getDriverName()) ;
System.out.println("Driver Version = " + dbmd.getDriverVersion()) ;
System.out.println("Database URL = " + dbmd.getURL()) ;
System.out.println("----------------------------------------------------") ;
/*
-------------the result from the above ----------
Database Name = Apache Derby
Database User Name= APP
Database Version = 10.14.1.0 - (1808820)
Driver Name = Apache Derby Embedded JDBC Driver
Driver Version = 10.14.1.0 - (1808820)
Database URL = jdbc:derby:elsarhEmbedded
*/
} catch (SQLException ex) {
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
我试图从 netbeans 对数据库执行命令并将数据插入同一个表中,它可以工作。
【问题讨论】:
-
URL
jdbc:derby:elsarhEmbedded是一个相对 url,您可能连接到与NetBeans 不同的数据库。您的应用程序使用的数据库可能是空的并且没有表。 -
@MarkRotteveel 我看到它在那里记录(db.apache.org/derby/docs/10.0/manuals/develop/develop15.html),但如果它错了我该怎么办?设置绝对路径是个好主意吗?
-
我没有添加 'create=true;'到 url 以创建一个不同的,如果它不存在。
标签: java jdbc javafx-8 derby embedded-database