Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Hibernate Basics Data Persistence & ORMs Using JDBC to Connect to a Database

Baljot Malhi
Baljot Malhi
3,953 Points

Insert exactly one row into languages table in SQL connecting to a database

I do not know what is wrong with the code below can anyone help me out with this error it show when I run it. Bummer! It looks like you don't have an executeUpdate call that contains the proper INSERT statement. Check your SQL syntax, as well as column names and values.

Application.java
// Load class "org.sqlite.JDBC"
Class.forName("org.sqlite.JDBC");

// Establish connection to database named "treehouse.db"
try(Connection c = DriverManager.getConnection("jdbc:sqlite:treehouse.db") ){
      Statement statement = c.createStatement();
      statement.executeUpdate( "DROP TABLE IF EXISTS languages");
            statement.executeUpdate("CREATE TABLE languages (id INTEGER PRIMARY KEY, name STRING" );
      statement.executeUpdate(" INSERT INTO languages (id, name) "+
                             " VALUES( 2, 'Java')");
} catch(SQLException ex) {
      System.err.printf("There was a database error: %s%n",ex.getMessage());
}
Iris Guo
Iris Guo
4,523 Points

I have the same question! Have you found the answer?

Iris Guo
Iris Guo
4,523 Points

I see. We should delete the space before the INSERT keyword to pass the online judge.

I think so too. Try deleting the space before "INSERT INTO languages". If that doesn't work try to remove the concatenation operator and write it as one query.

Benjamin Smith
Benjamin Smith
18,184 Points

Class.forName("org.sqlite.JDBC");

try(Connection c = DriverManager.getConnection("jdbc:sqlite:treehouse.db") ){ Statement statement = c.createStatement(); statement.executeUpdate( "DROP TABLE IF EXISTS languages");

// You were missing ")" after "STRING"

  statement.executeUpdate("CREATE TABLE languages (id INTEGER PRIMARY KEY, name STRING)" );

// You do not need to concatenate your SQL call.

  statement.executeUpdate("INSERT INTO languages (id, name) VALUES( 2, 'Java')");

} catch(SQLException ex) {

}