【问题标题】:JDBC returns null value in resultSet using where condition. I tried to get all columns but its not returning any valueJDBC 使用 where 条件在 resultSet 中返回空值。我试图获取所有列,但它没有返回任何值
【发布时间】: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


【解决方案1】:

我认为这不是 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,调用HashMapget() 方法会为不在映射中的键返回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(); 行。

【讨论】:

  • 我解决了这个问题...谢谢大家
猜你喜欢
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 2014-09-27
相关资源
最近更新 更多