Derby Schema Error

江枫思渺然 提交于 2019-12-02 16:38:23

问题


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);
    }
}                                       


}

回答1:


According to the Frequently Asked Questions of Apache Derby, you will see that:

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

In your code, you need create the table first and then work like a charm.

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");

The output is:

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.




回答2:


the easiest solution is to configure your database properties and make schema the same as user name but in capital litters ex: schema APP user app

hope my answer can help.



来源:https://stackoverflow.com/questions/16493422/derby-schema-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!