gpt4 book ai didi

java - Derby 架构错误

转载 作者:行者123 更新时间:2023-12-01 23:49:38 25 4
gpt4 key购买 nike

我正在尝试将数据插入我的桌面应用程序的 Derby 嵌入式数据库。但是 Derby 给了我架构未找到错误的错误。我尝试通过根据用户名创建架构来解决错误,但没有解决我的问题。我搜索了互联网,但没有一个解决方案解决了我的问题。

package derbyd.ui;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class AddStudent extends javax.swing.JFrame {

private void bt_saveActionPerformed(java.awt.event.ActionEvent evt) {
String conURL = "jdbc:derby:myDB;create=true";
String user = "SOURABH";
String passwd = "pass";
Connection con = null;
Statement st = null;
String query;
ResultSet rs = null;
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection(conURL, user, passwd);
st = con.createStatement();
System.out.println("Connection established");
query = "INSERT INTO SOURABH.MyTable VALUES('"+txt_name.getText()+"','"+txt_surname.getText()+"')";
st.executeUpdate(query);
System.out.println("Added Successfully");
}
catch(Exception e)
{
System.out.println("Error ->"+e);
}
}


}

最佳答案

根据Frequently Asked Questions的 Apache Derby,您会看到:

A schema is only created by CREATE SCHEMA or creating an object (table etc.) in that schema (this is implicit schema creation).

在代码中,您需要先创建表,然后才能正常工作。

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection(conURL, user, passwd);
st = con.createStatement();
System.out.println("Connection established");

st.executeUpdate("CREATE TABLE MyTable" +
"(name VARCHAR(255), surname varchar(255))");
System.out.println("Table created");

query = "INSERT INTO SOURABH.MyTable VALUES(" +
"'" + txt_name.getText() + "','" + txt_surname.getText() + "')";
st.executeUpdate(query);
System.out.println("Added Successfully");

输出为:

Connection established
Table created
Added Successfully

如果多次运行此语句,您将收到错误,因为该表已存在。为了解决这个问题,here .

关于java - Derby 架构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16493422/

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