2011-12-31 8 views
0

私はSpring Frameworkを使用してアプリケーションを作成しています。JDBC/SpringでSQL結果セットエラーを解決する

練習問題の説明:データベースを作成できるプログラムを作成します。データベースには、法律学校に関する情報が含まれます。私はロースクール、クエリなどを追加するためにこれを使うことができます。私のステートメントが結果セットを生成しないことを示すエラーメッセージが表示されます。私の知識は正しいと思われますが、私の知識は明らかに欠陥があります。私は、私のapplication.xmlファイルが正しく書かれていることを徹底的に確信していますので、ここには含めません。

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<School> getSchools() 
{ 
    try 
    { 
     Connection con = null; 
     PreparedStatement selectAllSchools = null; 
     ResultSet allSchools = null; 
     List<School> schools = new ArrayList<School>(); 
     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<School> getByRank(int rank) 
{ 
    return null; 
} 

public List<School> 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."); 
    } 
} 
} 

クライアントテスト:

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<School> allLawSchools = service.getAllSchools(); 
    for (School school : allLawSchools) 
     System.out.println(school); 
} 
} 

エラーメッセージ:

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 
+0

のための特別なSQL文をGET' 'ですHSQLDB? – Mat

+0

私が知っているわけではありません。あなたが具体的に言及していることを教えてくれますか? – harryo

+0

SELECT_ALL_SCHOOLS変数... – Mat

答えて

1

でライン

private static final String SELECT_ALL_SCHOOLS = "GET * FROM lawschools"; 

を交換してみてください

private static final String SELECT_ALL_SCHOOLS = "SELECT * FROM lawschools"; 

または、より良いまだ、

private static final String SELECT_ALL_SCHOOLS = "SELECT name, city, state, rank FROM lawschools"; 

EDIT:データベースに名前、市、州およびランク値を送信していないので、あなたの学校が移植されていません。挿入するためのコードは(私がget...メソッドの名前について確認することはできませんので、私はあなたのLawSchoolクラスを持っていない)、次のようなものになります。

insertSchool = con.prepareStatement(INSERT_SCHOOL); 
    insertSchool.setString(1, newSchool.getName()); 
    insertSchool.setString(2, newSchool.getState()); 
    insertSchool.setString(3, newSchool.getCity()); 
    insertSchool.setInt(4, newSchool.getRank()); 
    insertSchool.executeUpdate(); 
+0

はい私はあなたの最初の勧告でSELECT_ALL_SCHOOLSを置き換えました。結果セットの問題は今解決されました。私にとって解決しなければならない問題は、データベースに新しい行を追加することです。 – harryo