java - JDBC 使用 where 条件在 resultSet 中返回空值。我试图获取所有列,但它没有返回任何值
问题描述
我想使用 where 条件从数据库中获取所有数据。但是 resultSet 返回空值。但是当我使用整数而不是字符串时,它可以正常工作,但我不想要。
我不是很多 SQL 专家,但是当我在 SQL Server 中运行查询时,它工作正常。
public JSONObject searchInternship1(String Category) throws SQLException {
ResultSet result;
Connection con = null;
PreparedStatement stmt = null;
JSONArray jsonArray = new JSONArray();
JSONObject mainObject1 = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@144.217.163.57:1521:XE", "mad310team2", "anypw");
String sql;
sql = ("SELECT * FROM INTERNSHIP WHERE CATEGORY= ?");
stmt = con.prepareStatement(sql);
// con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
stmt.setString(1, Category);
result = stmt.executeQuery();
System.out.println("resultttt " + result);
String status;
Instant instant = Instant.now();
long time = instant.getEpochSecond();
if (result.next() == false) {
status = "Failed";
mainObject1.accumulate("Status :", status);
mainObject1.accumulate("Timestamp :", time);
mainObject1.accumulate("Message :", " Fetching Failed");
mainObject1.accumulate("why not", Category);
// System.out.println("hellooooo "+ result);
} else {
do {
mainObject1.accumulate("why not111", Category);
status = "Success";
jsonObject.accumulate("Id :", result.getInt("INT_ID"));
jsonObject.accumulate("CONTACT PERSON", result.getString("CONTACT_PERSON"));
jsonObject.accumulate("INCENTIVE", result.getString("INCENTIVE"));
jsonObject.accumulate(" VENUE", result.getString("VENUE"));
jsonObject.accumulate("DATE OF TEST", result.getDate("DATE_OF_TEST").toString());
jsonObject.accumulate("DATE OF TEST", result.getString("TIME_OF_TEST"));
jsonObject.accumulate("PROCEDURE", result.getString("PROCEDUREE"));
jsonObject.accumulate("No. OF VACANCIES", result.getInt("NO_OF_VACANCIES"));
jsonObject.accumulate("COMPANY ID", result.getInt("COMPANY_ID"));
jsonObject.accumulate("CATEGORY", result.getString("CATEGORY"));
jsonArray.add(jsonObject);
jsonObject.clear();
} while (result.next());
// result = result + r.getString("data");
mainObject1.accumulate("Details of jobs: ", jsonArray);
}
stmt.close();
con.close();
} catch (SQLException ex) {
// System.out.println("Error at111:" + ex.getClass().getName() + ex.getMessage());
Logger.getLogger(Register_Detail.class.getName()).log(Level.SEVERE, null, ex);
}
return mainObject1;
}
@GET
@Path("searchInternship&{value1}")
@Produces(MediaType.TEXT_PLAIN)
public String searchInternship(@PathParam("value1") String Category)
throws SQLException {
JSONObject result = searchInternship1(Category);
return result.toString();
}
解决方案
我不相信这是一个 JDBC 问题。我相信这是您如何从结果集中读取数据的问题。
在您的createInternship1
方法的顶部,您创建一个 JSONObject 对象。假设至少有一行从结果集中返回。对于从结果集中读取的第一行,您的代码然后从结果集中读取值并将它们写入您的 JSONObject,然后再将您的 JSONObject 添加到您的 JSON 数组jsonArray
。
到目前为止,您的代码正在做您想做的事情。您有一个 JSON 数组,其中包含一个对象,其中包含从结果集中第一行读取的数据。但是,问题出现在您的下一步中。
然后清除您的 JSONObject。
您的数组不包含 JSONObject 的副本,它包含相同的对象。
所以此时,您的数组现在包含一个空 JSONObject。您从结果集中读出的数据已被删除。
当您的代码继续遍历结果集中的更多行时,它会遇到另一个问题。它将更多数据读取到之前创建的同一个 JSONObject 中,将同一个 JSONObject(已经在数组中)添加到数组中,然后再次清除同一个 JSONObject。因此,对于从结果集中读取的每一行,您最终会得到一个包含相同空 JSONObject 的 JSON 数组。
我认为您正在使用 json-simple 库来处理 JSON。最初我以为你使用的是 org.json,但是那个库中的 JSONObject 类没有clear()
方法,而 json-simple 的 JSONObject 类确实有这个方法。json-simple 的 JSONObject 类扩展java.util.HashMap
,并调用 a 的get()
方法HashMap
返回null
一个不在地图中的键。您的一个 JSONObject 是空的,因此映射中没有键,因此null
将始终从get()
对该对象的方法的任何调用中返回。这有望解释为什么您会返回空值。
您可以通过为循环的每次迭代do ... while
而不是在顶部创建一个新的 JSONObject 来解决此问题,将其添加到 JSON 数组中,并删除清除 JSON 对象的行:
do {
JSONObject jsonObject = new JSONObject(); // add this line
mainObject1.accumulate("why not111", Category);
status = "Success";
jsonObject.accumulate("Id :", result.getInt("INT_ID"));
jsonObject.accumulate("CONTACT PERSON", result.getString("CONTACT_PERSON"));
// ... various similar lines omitted for clarity
jsonObject.accumulate("CATEGORY", result.getString("CATEGORY"));
jsonArray.add(jsonObject);
} while (result.next());
您还需要JSONObject jsonObject = new JSONObject();
在代码中进一步删除该行。
推荐阅读
- r - 对于运行 R 3.6.0 的 Windows 10,Rstudio 1.2.1335 中的 R grep() 函数存在问题
- laravel - Laravel - 仅搜索用户 id 帖子
- node.js - 在 Node.js 中,使用 `{shell: true}` 生成一个进程,然后与实际进程而不是 shell 进行通信
- arcgis - ArcGIS RuntimeError:未登录门户
- amazon-web-services - React Native:将视频发送到 S3 时压缩视频
- flutter - 即使不在当前页面中,Flutter redux 也会不断刷新小部件
- c# - 如何投射 CreateDelegate 并分配给 Action
- python - 是否有某种方法可以编写带有两个布尔值的 If 语句?
- python - 如何提高生物信息学脚本的速度?
- vue.js - DOM中存在多个实例时如何选择Vue组件的特定实例