- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个连接到 apache derby 数据库的 java 类。突然停止工作,我不知道为什么。
基本上,客户端连接到服务器,服务器调用“联系人”类,然后联系人类查询数据库并返回结果。
这是 Contact 类的代码:
import java.sql.*;
//Contact Class. Used to get information from the contact database.
//Takes the database connection and the student ID as arguments
public class Contact {
//Define the original variables
String student = null;
Connection db = null;
PreparedStatement selectStatement = null;
ResultSet resultSet = null;
StringBuilder result = null;
//Constructor method. Used to set passed arguments to class variables.
public Contact(Connection conn, String studentNumber)
{
this.student = studentNumber;
this.db = conn;
}
//getResult method used to query the database and return result.
public String getResult()
{
//Wrap it all in a try loop to catch sql errors.
try {
//Set up the statement, prepare the statement and set the variable.
String selectSQL = "SELECT * FROM Contact WHERE STUID = ?";
selectStatement = db.prepareStatement(selectSQL);
selectStatement.setString(1, this.student);
//Execute the query
resultSet = selectStatement.executeQuery();
//If there is no results, set up a string to return letting the user know.
this.result = new StringBuilder();
if(!resultSet.next())
{
result.append("No record for ");
result.append(this.student);
result.append(".");
}
else
{
//If there are results, loop through the result and build a string
//To be able to return to the user with the correct details.
System.out.println("FOUND A RESULT");
while(resultSet.next())
{
System.out.println("TEST");
System.out.println(resultSet.getString("STUID"));
result.append(resultSet.getString("STUID"));
result.append(" ");
result.append(resultSet.getString("STUNAME"));
result.append(" ");
result.append(resultSet.getString("ADDRESS"));
result.append(" ");
result.append(resultSet.getString("POSTCODE"));
result.append(" ");
result.append(resultSet.getString("EMAIL"));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Return the built string with correct information.
return this.result.toString();
}
}
如果我输入的 STUID 不在数据库中,它会通过返回“No Record For ID”成功地让我知道。但是,如果我输入数据库中的一个,它会打印“FOUND A RESULT”行(只是一个测试行),但从未真正到达“TEST”输出 - 因此永远不会用结果构建任何字符串。
我知道这不是数据库,因为我在线程类中测试了一些代码(在调用此联系人类之前),并且查询相同的数据库有效:
Statement s = null;
ResultSet rs = null;
try{
s = dbConnection.createStatement();
rs = s.executeQuery("SELECT * FROM Contact");
while(rs.next())
{
System.out.println(rs.getString("STUID"));
}
}catch(SQLException e)
{
e.printStackTrace();
}
该测试代码有效。
所以我真的很困惑为什么它可以成功查询数据库,并发现没有结果(通过使用 if(!resultSet.next() ),但如果发现实际上有一个结果它无法循环给我详细信息。任何帮助将不胜感激!
最佳答案
发生这种情况是因为您正在越过结果集中的第一个结果:
if(!resultSet.next())
{
result.append("No record for ");
result.append(this.student);
result.append(".");
}
else // <-- failurehere
{
你的 else 隐式调用 resultSet.next()
,它将移过第一个元素。如果您查询包含两个元素的内容,您只会返回第二个元素。
关于Java resultset.next() 不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23484311/
在我的应用程序中,我必须获取记录并将它们放入二维数组中。我必须首先触发两个查询以找出计数,以便我可以初始化数组,其次是获取数据。它会导致性能下降。我需要提高性能的解决方案。 谢谢。 最佳答案 I ha
在 Zend Framework 应用程序中,它的构建与 ZF2 Getting Started tutorial 中的相册应用程序非常相似,我使用 ResultSet 对象将数据从模型通过 Cont
我是 java 和 SQL 新手,我重复了一个我不知道如何避免的问题: 假设我想创建两个executeQuery,一个在另一个内部,在getRequestsFromDB方法中我创建第一个execute
这个问题已经有答案了: Why should I call close() on a ResultSet and Connection instance? (4 个回答) 已关闭 7 年前。 使用完结
public List searchCourse(String name) { List list = null; Course course = null;
我正在使用两个不同的查询从两个表中选择数据。我正在使用一个连接和一个结果集。两个表都已填充,但第二个查询的 resultset.next() 返回 false,尽管它必须为 true。 我还尝试使用两
试图了解这里发生了什么。 DatabaseMetaData 返回一个空结果集,而实际上相同的 SQL 查询则不会。这不是主要问题,因为我使用第二个代码示例作为解决方法。 DatabaseMetaDat
假设我有多个结果集(每个结果集将引用数据库中的 1 行)(它们属于同一个表。)。现在我想创建合并的结果集,它将实习生拥有所有其他结果集。所以我的主要目标是创建一个组合的 ResultSet,它将指向以
我正在尝试将 influxdb 与我的应用程序集成并处理输出。我正在导入 InfluxDBClient 包以连接到在我的本地计算机上运行的 influx 实例。使用以“influxdb.results
我正在尝试创建一个框架来显示数据库中的所有电影名称,然后他们选择框架中的一行来执行另一个查询。 我有一个 ResultSet (rs),它接收查询执行的结果以选择电影名称。之后,我使用 SQLTabl
我有一个 Servlet,它使用返回 ResultSet 的静态实用程序方法。 在我的 Servlet 代码中,我有 ResultSet rs = SimpleSearch.searchByName(
在从 Postgres 或 Oracle 检索时间戳时(我们称之为 startDate),使用 resultSet.getString("startDate") 调用或 resultSet.getTi
全部, 对于名为Analyze 的类及其构造函数Analyze,该构造函数将Java JDBC Resultset 对象作为参数。 private Resultset analysisSet publ
m 尝试遍历 3 个结果集并比较它们的值。 bt 它抛出这个异常......有人可以帮助我了解我正在经历的地方吗?这是一段代码: java.lang.Object[] reconciledPaymen
我已经尝试使用谷歌搜索并在 SO 上查看了这些线程: java jdbc accessing multiple resultsets Open two resultsets simultaneousl
我在 SQLite 的 JDBC 驱动程序中遇到问题。 我正在使用 SELECT 语句执行查询。 如果我得到一个空的 ResultSet(0 行),那么我会在调用 getString(1) 时看到抛出
我正在尝试根据特定条件搜索表格。在 SQL Developer 中执行时,SQL 查询返回正确的结果(1 行)。在 JDBC 中,resultset 不为 null,但 resultset.next(
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在尝试在 Java 应用程序中处理大量数据。数据存储在 MySQL 数据库中,我使用的是 jdbc 连接器 8.0.11。 我的问题是我需要多次访问每条记录并且再次执行查询需要太多时间。使用 Re
这个问题已经有答案了: Getting java.sql.SQLException: Operation not allowed after ResultSet closed (2 个回答) 已关闭
我是一名优秀的程序员,十分优秀!