当前位置: 代码迷 >> java >> Java ResultSet通常返回空字段
  详细解决方案

Java ResultSet通常返回空字段

热度:107   发布时间:2023-08-02 10:47:40.0

我在一个结果集上苦苦挣扎,该结果集通常显示应该有值的空字段(我在Oracle SQL dev中测试了查询)。 这很奇怪,因为正确提取了我的一个字段(“ openDate”-> rs.getDate(5)),而其他字段却没有。 这是我的代码:

public void refreshData() {

        try {

            Globals.itrackDatabase.connect();

            ResultSet rs;
            rs = Globals.itrackDatabase.execQuery(Globals.caseRankingQuery + " WHERE case_num = '19816641'");

//          ResultSetMetaData metaData = rs.getMetaData();
//            int numberOfColumns = metaData.getColumnCount();

            while (rs.next()) {

                System.out.println(rs.getString(1));
                Case caseObj = new Case(rs.getString(1), rs.getString(2), rs.getString(3), 
                        rs.getString(4), rs.getDate(5), rs.getString(6), rs.getString(7), 
                        rs.getString(8), null, rs.getString(9), rs.getString(10), 0);

                Globals.caseList.add(caseObj);
            }

            for (int i = 0; i <= Globals.caseList.size()-1; i++) {

                data[i][1] = Globals.caseList.get(i).id;
                data[i][2] = Globals.caseList.get(i).service;
                data[i][3] = Globals.caseList.get(i).module;
                data[i][4] = Globals.caseList.get(i).summary;
                data[i][5] = Globals.caseList.get(i).openDate;
                data[i][6] = Globals.caseList.get(i).hub;
                data[i][7] = Globals.caseList.get(i).endUser;
                data[i][8] = Globals.caseList.get(i).endUserGroup;
                data[i][9] = Globals.caseList.get(i).assignationDate;
                data[i][10] = Globals.caseList.get(i).timeSpent;
                data[i][11] = Globals.caseList.get(i).assignee;
                data[i][12] = Globals.caseList.get(i).assigneeBacklog;
            }

            Globals.itrackDatabase.disconnect();
            System.out.println(Arrays.deepToString(data));
            this.fireTableDataChanged();

        } catch (SQLException e) {
            System.out.println("Query failed.");
            e.printStackTrace();
        }
    }

和:

public ResultSet execQuery(String query) {

        try {
            Statement stmt = connection.createStatement();
            System.out.println("Executing: " + query);
            return stmt.executeQuery(query);
        } catch (SQLException e) {
            System.out.println("Query failed.");
            e.printStackTrace();
            return null;
        }
    }

案例对象构造函数:

public Case(String id, String service, String module, String summary, 
                Date openDate, String hub, String endUser, String endUserGroup, 
                Date assignationDate, String timeSpent, String assignee, int assigneeBacklog) {

        this.id = id;
        this.service = service;
        this.module = module;
        this.summary = summary;
        this.openDate = openDate;
        this.hub = hub;
        this.endUser = endUser;
        this.endUserGroup = endUserGroup;
        this.endUserGroup = endUserGroup;
        this.assignationDate = assignationDate;
        this.timeSpent = timeSpent;
        this.assignee = assignee;
        this.assigneeBacklog = assigneeBacklog;
    }

查询是:

SELECT case_num, service, module, summary, open_date, case_hub, end_user, end_user_site, end_user_dept, time_spent, assignee 
FROM CASE 
WHERE case_num = '19816641';

我的控制台调试:

-------- Oracle JDBC Connection Testing ------
Connected to Itrack database.
Executing: SELECT case_num, service, module, summary, open_date, case_hub, end_user, end_user_site, end_user_dept, time_spent, assignee FROM CASE WHERE case_num = '19816641'

[[null, , , null, , 2015-07-19, , , , null, , , 0]]

好的,找到它:我必须使用结果集中的ascii流获取器,然后创建以下函数将其更改为字符串:

public String getStringFromInputStream(InputStream is) {

        InputStreamReader in = new InputStreamReader(is);
        String str = "";

        try {
            while (in.ready()) {
                str = str + (char) in.read();  
            }
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

您这样称呼它:

getStringFromInputStream(rs.getAsciiStream(1))

希望这可以帮助。

  相关解决方案