Search code examples
javadatabasejdbcjtablederby

How do I add a new row of data into my Derby database?


Using Netbeans, I have my database and table set up, and have added my data manually, in which I am able to see within my application I am building, as intended.

I would like the user to add their own data in which will be appended to a new row on the table. However, I am having trouble trying to write code in order to do this.

        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/stockApplication");
        Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        String insertDerbyData = "INSERT INTO TIGER_INFO" 
                    + "(TIGER_ID, TIGER_LOCATION)"
                    + "VALUES (123456, Store)";
        stat.executeUpdate(insertDerbyData);

I cannot execute the above code as I'm returned with an error mentioning that 'STORE' is not in any table. 'STORE' is meant to be a value for my 'TIGER_LOCATION' column. What's going on here?

In theory, I have two columns, and I would like to add both values, '123456' and 'Store' into their respective columns. How do I go about correctly doing so?


Solution

  • If TIGER_LOCATION is a string/varchar column, and Store is a string literal, then the value must be enclosed in single quotes, as in most SQL-based databases:

    INSERT INTO TIGER_INFO (TIGER_ID, TIGER_LOCATION) VALUES (123456, 'Store')