Derby模式错误 [英] Derby Schema Error
问题描述
我要为我的桌面应用程序插入数据到Derby嵌入式数据库。但是Derby给我错误的模式未找到错误。
我试图通过创建模式为每个用户名解决错误,但不解决我的问题。我搜索互联网,但没有给定的解决方案解决了我的问题。
I am trying to insert data into Derby embedded database for my Desktop Application. But Derby is giving me error of Schema Not found error. I have tried to solve error by creating schema as per username, but does not solve my problem. I searched internet, but none of given solution solved my problem.
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);
}
}
}
推荐答案
根据常见问题 Apache Derby,您将看到:
According to the Frequently Asked Questions of Apache Derby, you will see that:
模式只能由
CREATE SCHEMA
或在该模式中创建对象(表等)(这是隐式模式创建)。
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
如果您多次运行此语句,则会收到错误,因为表已经存在。要处理此问题,请此处。
If you run this statement more than once, you'll get an error because the table already exists. To deal with that, here.
这篇关于Derby模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!