Note that there are some explanatory texts on larger screens.

plurals
  1. POSolving an SQL Result Set Error with JDBC/Spring
    primarykey
    data
    text
    <p>I'm writing an application using the Spring Framework. </p> <p>Description of Exercise: Write a program that will let me create a database. The database will contain information about law schools. I can use this to add law schools, query, etc. I am getting an error message stating that my statements don't generate a Result Set. My code appears to be correct according to my knowledge, but my knowledge is obviously flawed. I am thoroughly convinced that my application.xml file has been written correctly, so I will not include it here. </p> <pre><code>import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.sears.domain.School; public class SchoolDaoImplementation implements SchoolDao { private static final String USERNAME = "sa"; private static final String PASSWORD = ""; private static final String CREATE_TABLE = "CREATE TABLE lawschools (name VARCHAR(15) NOT NULL PRIMARY KEY, city VARCHAR(15), state VARCHAR(2), rank INTEGER)"; private static final String INSERT_SCHOOL = "INSERT INTO lawschools (name, city, state, rank) VALUES (?, ?, ?, ?)"; private static final String SELECT_ALL_SCHOOLS = "GET * FROM lawschools"; private static final String DATABASE_URL = "jdbc:hsqldb:file:database.dat;shutdown=true"; private static final String DRIVER_NAME = "org.hsqldb.jdbcDriver"; public SchoolDaoImplementation() { try { Class.forName(DRIVER_NAME); createTable(); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("School DAO implementation instantiated."); } private static void createTable() { try { Connection con = null; PreparedStatement createTable = null; try { con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); createTable = con.prepareStatement(CREATE_TABLE); createTable.executeUpdate(); System.out.println("Creted Table."); } finally { if (con != null) con.close(); if (createTable != null) createTable.close(); } } catch (SQLException e) { System.out.println("Assuming table has been created."); } System.out.println("Table created successfully."); } public School getSchool(String name) { return null; } public List&lt;School&gt; getSchools() { try { Connection con = null; PreparedStatement selectAllSchools = null; ResultSet allSchools = null; List&lt;School&gt; schools = new ArrayList&lt;School&gt;(); try { con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); selectAllSchools = con.prepareStatement(SELECT_ALL_SCHOOLS); allSchools = selectAllSchools.executeQuery(); while (allSchools.next()) { String name = allSchools.getString(1); String city = allSchools.getString(2); String state = allSchools.getString(3); int rank = allSchools.getInt(4); schools.add(new School(name, city, state, rank)); } return schools; } finally { if (con != null) con.close(); if (selectAllSchools != null) selectAllSchools.close(); if (allSchools != null) allSchools.close(); } } catch (SQLException e) { throw new RuntimeException(e); } } public List&lt;School&gt; getByRank(int rank) { return null; } public List&lt;School&gt; getByState(String state) { return null; } public void addSchool(School newSchool) { try { Connection con = null; PreparedStatement insertSchool = null; try { con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); insertSchool = con.prepareStatement(INSERT_SCHOOL); insertSchool.executeUpdate(); } finally { if (con != null) con.close(); if (insertSchool != null) insertSchool.close(); } } catch (SQLException e) { System.out.println("An error has occured."); } } } </code></pre> <p>Client Test: </p> <pre><code>public class ClientTest { public static void main(String[] args) { ApplicationContext container = new ClassPathXmlApplicationContext("application.xml"); RankingService service = (RankingService) container.getBean("rankingServiceProduction"); System.out.println("Welcome to the LawSchool Ranking Service\n"); service.addNewSchool(new LawSchool("Duke", "Durham", "NC", 11)); service.addNewSchool(new LawSchool("Northwestern", "Chicago", "IL", 11)); service.addNewSchool(new LawSchool("Cornell", "Ithaca", "NY", 13)); service.addNewSchool(new LawSchool("Georgetown", "District of Columbia", "DC", 14)); List&lt;School&gt; allLawSchools = service.getAllSchools(); for (School school : allLawSchools) System.out.println(school); } } </code></pre> <p>Error Message:</p> <pre><code>Exception in thread "main" java.lang.RuntimeException: java.sql.SQLException: Statement does not generate a result set at com.sears.data.SchoolDaoImplementation.getSchools(SchoolDaoImplementation.java:107) at com.sears.services.RankingServiceProduction.getAllSchools(RankingServiceProduction.java:24) at com.sears.client.ClientTest.main(ClientTest.java:22) Caused by: java.sql.SQLException: Statement does not generate a result set at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.jdbcPreparedStatement.checkIsRowCount(Unknown Source) at org.hsqldb.jdbc.jdbcPreparedStatement.executeQuery(Unknown Source) at com.sears.data.SchoolDaoImplementation.getSchools(SchoolDaoImplementation.java:84) ... 2 more </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload