Derby Schema Error

前端 未结 2 1114
情歌与酒
情歌与酒 2021-01-28 23:47

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 tri

2条回答
  •  伪装坚强ぢ
    2021-01-29 00:15

    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.

提交回复
热议问题