Results 1 to 9 of 9
  1. #1
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default Need help with derby database connection/insert

    Hi guys I'm currently using netbeans and need to make a program for school where the user is able to store questions he's written in a database. I have connected to the database well and have no errors except when I try to save the information to the database here is my code:

    String myInsertsql = " VALUES(\'" + newquestion1.getQuestion() + "\', "
    + "\'" + newquestion1.getQuestionNo() + "\', \'" + newquestion1.getAnswerA() + "\', " +
    "\'" + newquestion1.getAnswerB() + "\', " + "\'" + newquestion1.getAnswerC() + "\', " + "\'" + newquestion1.getAnswerD() + "\')";


    try
    {
    Connection con = DriverManager.getConnection(databaseURL, "user", "password");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("INSERT INTO QUESTIONS (QUESTION, QUESTIONNUMBER, ANSWER, ANSWERA, ANSWERB, ANSWERC, ANSWERD)" + myInsertsql);

    con.close();
    } catch (SQLException errorObject)
    {
    JOptionPane.showMessageDialog(null, "ERROR: "+ errorObject);
    }





    String databaseURL = "jdbc:derby://localhost:1527/questions";

    public main() {
    initComponents();

    try
    {
    Class.forName("org.apache.derby.jdbc.ClientDriver" );

    }catch (ClassNotFoundException e)
    {
    JOptionPane.showMessageDialog(null, e);
    }


    I tried to align it the best I could I get an error saying: Scheme 'USER' does not exist would be awesome if anyone can fix it.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Unless you put it in [code][/code] tags it will not save the formatting.

    What error do you get when you try to save?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Post an SSCCE that demonstrates the problem, along with the full error message as Dark mentioned. Secondly, you might consider using a PreparedStatement
    Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javaguy345 View Post
    I tried to align it the best I could I get an error saying: Scheme 'USER' does not exist would be awesome if anyone can fix it.
    I wonder how you've created your table; tables belong in a schema; have you created a schema first and created a table in that schema?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default

    I'm kind of lost can you link me to some info about schemas? The error code says something about schema and user iI got it on my OP.
    EDIT: In properties it said my schema is in APP, which my table is in.
    Last edited by javaguy345; 05-18-2011 at 04:52 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javaguy345 View Post
    I'm kind of lost can you link me to some info about schemas? The error code says something about schema and user iI got it on my OP.
    EDIT: In properties it said my schema is in APP, which my table is in.
    Ok, then the full name of your table is APP.QUESTIONS. Change the text of your SQL query.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default

    Okay now I'm receiving a different error message it says Columns type of Numeric cannot hold type CHAR. But I'm sure I'm passing an int into the numeric column

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javaguy345 View Post
    Okay now I'm receiving a different error message it says Columns type of Numeric cannot hold type CHAR. But I'm sure I'm passing an int into the numeric column
    You think you're passing a numeric value but obviously you're not; print the complete SQL query before you execute it and see what you're feeding to the database; there's an error there (pay very close attention to the quotes).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    You are surrounding every one of your values in quotes, so essentially saying they're CHARs.

    This should be a PreparedStatement, so you wouldn't have to worry about quotes at all. Your SQL would become:

    INSERT INTO QUESTIONS (QUESTION, QUESTIONNUMBER, ANSWER, ANSWERA, ANSWERB, ANSWERC, ANSWERD)VALUES(?,?,?,?,?,?,?)
    (assuming I've counted my ? correctly)

    You would then use the relevant setXXX() method for each value ('?').

Similar Threads

  1. Deploy derby database
    By Muaz.Sh in forum NetBeans
    Replies: 11
    Last Post: 03-17-2011, 06:02 PM
  2. how to access derby database with servlets
    By Mateen in forum Java Servlet
    Replies: 0
    Last Post: 07-12-2010, 09:16 PM
  3. Using Derby Database in Netbeans
    By DavidG24 in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 01:29 PM
  4. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  5. saving date and time in a derby database from an ejb module
    By Dave in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-22-2008, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •