gpt4 book ai didi

java嵌入式derby表/ View

转载 作者:行者123 更新时间:2023-12-02 08:23:35 24 4
gpt4 key购买 nike

我创建了嵌入式 Derby 数据库,它给了我错误。尽管我有在其中创建表 REST 的 APP 模式

java.sql.SQLSyntaxErrorException: Table/View 'REST' does not exist.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)

这是java类:

public class Main
{
private static String dbURL = "jdbc:derby:tes;create=true";
private static String tableName = "REST";
// jdbc Connection
private static Connection conn = null;
private static Statement stmt = null;

public static void main(String[] args)
{
createConnection();
insertRestaurants(5, "LaVals", "Berkeley");
selectRestaurants();
shutdown();
}

private static void createConnection()
{
try
{
// System.setProperty("derby.system.home", "/Users/myuser/futbol");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//Get a connection
conn = DriverManager.getConnection(dbURL);
}
catch (Exception except)
{
except.printStackTrace();
}
}
private static void insertRestaurants(int id, String restName, String cityName)
{
try
{
stmt = conn.createStatement();
stmt.execute("insert into REST values (" +
id + ",'" + restName + "','" + cityName +"')");
stmt.close();
}
catch (SQLException sqlExcept)
{
sqlExcept.printStackTrace();
}
}

}

最佳答案

我知道我在这方面落后了四年,但我终于找到了如何解决我的问题(这就是为什么我在这个帖子上)。

我使用数据库脚本编辑器创建了表。我在 table 周围使用了双引号。当我进入服务选项卡(netbeans)并右键单击我的表以查看数据时,我发现了这一点。我得到了这个:

select * from {schema}."{table}"

所以,我想我应该把它翻译成我的java.lang.还有,宾果。希望这会对您的应用程序有所启发,我复制并粘贴了您的一些代码,希望这会起作用。

public class Db {
private final String url = "jdbc:derby://localhost:1527/{db}";
private final String tab = "{schema}.\"REST\"";

private static Connection createConnection() throws Exception {
Connection conn = null;
conn = DriverManager.getConnection(url);
return conn;
}

private static void insertRestaurants(int id, String restName, String cityName)
{
try
{
Connection conn = createConnection();
stmt = conn.createStatement();
stmt.execute("insert into " + table + " values (" +
id + ",'" + restName + "','" + cityName +"')");
stmt.close();
}
catch (SQLException sqlExcept)
{
sqlExcept.printStackTrace();
}
}
}

干杯!

关于java嵌入式derby表/ View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5007868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com