Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Ly Nguyen
Ly Nguyen
8,749 Points

How do you create a statement and execute a query using Java and SQL?

I believe I am following the right steps but the compiler says that it cannot find the symbol.

Application.java
// Load class "org.sqlite.JDBC"

// Establish connection to database named "treehouse.db"
try(Connection c = DriverManager.getConnection("jdbc:sqlite:treehouse.db")) {
  Class.forName("org.sqlite.JDBC");
  Statement stmt = null;
  stmt = c.createStatement();
  ResultSet rs = stmt.executeQuery("INSERT INTO languages (id, name) VALUES (2, 'Java')");
} catch(SQLException ex) {

}
Ly Nguyen
Ly Nguyen
8,749 Points

I was able to figure out my mistake. I should have run the executeUpdate method instead of executeQuery method. Also, there is no ResultSet created if you're not querying.

New code that passes:

// Load class "org.sqlite.JDBC"

// Establish connection to database named "treehouse.db"
try(Connection c = DriverManager.getConnection("jdbc:sqlite:treehouse.db")) {
  Class.forName("org.sqlite.JDBC");
  Statement stmt = c.createStatement();
  stmt.executeUpdate("INSERT INTO languages (id, name) VALUES (2, 'Java')");
} catch(SQLException ex) {

}