我正在使用 Spring 框架编写一个应用程序。
练习描述:编写一个程序来创建数据库。该数据库将包含有关法学院的信息。我可以使用它来添加法学院、查询等。我收到一条错误消息,指出我的语句不会生成结果集。根据我的知识,我的代码似乎是正确的,但我的知识显然有缺陷。我完全确信我的 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
最佳答案
尝试更换线路
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";
编辑:您的学校未填充,因为您没有将名称、城市、州和排名值发送到数据库。您的插入代码应如下所示(我没有您的 LawSchool
类,因此我无法确定 get...
方法的名称):
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();
关于java - 使用 JDBC/Spring 解决 SQL 结果集错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8690779/