【发布时间】:2020-03-12 05:32:38
【问题描述】:
我想使用 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();
}
【问题讨论】:
-
与您的问题无关,但您的代码不应调用
DriverManager.registerDriver,该方法将由JDBC驱动程序实现调用,而不是由普通代码调用。 -
当你说'resultSet返回空值'时,这是否意味着你的
searchInternship1方法返回(a)null,(b)nulls的数组,(c)一个数组空对象,(d) 对象数组,每个对象的属性都有一个null值,或者 (e) 其他什么?
标签: java sql jdbc where-clause