将Java结果集为String数组 [英] Convert Java resultset to String array

查看:170
本文介绍了将Java结果集为String数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序,将查询MS Access数据库,返回查询结果集,然后我希望最终将其转换结果集为一个String数组,这样我可以将它传递到的构造JComboBox的摆动 - 这样的组合框会列出该查询返回的项目

我已经能够到ArrayList中存储的结果集到一个ArrayList的行,然后转换成一个对象数组,组合框会列出正确的项目,但作为对象。我根本无法施展过的ArrayList是一个字符串数组。有谁知道这是否可能?下面是我的一些code的...

  //将结果集到一个数组列表公众的ArrayList< ArrayList的<对象>> Results2Array(ResultSet的RS)抛出的SQLException {
    ResultSetMetaData的元数据= rs.getMetaData();
    INT列= metaData.getColumnCount();    ArrayList的< ArrayList的<对象>>人=新的ArrayList< ArrayList的<对象>>();    而(rs.next()){
        ArrayList的<对象>记录=新的ArrayList<对象>();        的for(int i = 1; I< =列;我++){
            对象值= rs.getObject(ⅰ);
            record.add(值);
        }
        al.add(记录);
    }
    返回人;
}//转换到ArrayList的对象数组,并传递到GUIArrayList的<串GT;位置=新的ArrayList<串GT;();
ArrayList的<串GT;个月=新的ArrayList<串GT;();
ArrayList的<串GT;年=新的ArrayList<串GT;();尝试{
    DB.loadDriver();
    DB.makeConnection();
    DB.buildStatement();    位置= DB.getLocations();
    个月= DB.getMonths();
    年= DB.getYears();    [对象] arrLocations = Locations.toArray();
    [对象] arrMonths = Months.toArray();
    [对象] arrYears = Years.toArray();    dbGUI UI =新dbGUI(arrLocations,arrMonths,arrYears);
    ui.setVisible(真);

任何人都可以提供任何建议吗?谢谢!


更新:

下面是我收到的堆栈跟踪:

  java.lang.ArrayStoreException
    在java.lang.System.arraycopy(本机方法)
    在java.util.Arrays.copyOf(来源不明)
    在java.util.ArrayList.toArray(来源不明)
    在kidsfirstdb.Main.main(Main.java:23)


解决方案

 的String [] = arrLocations locations.toArray(新的String [0]);

是正确的答案。原因你的例外是,所有的对象其实不是字符串。

您需要更改此:

 对象值= rs.getObject(I)

这样:

 字符串值= rs.getString(I)

或本

 字符串值= rs.getObject(I)的ToString();

这是最后一个需要一个空检查,如果你可以返回null列。

注意的toString()再presentation可能不是你在所有的情况下寻找什么,但它会帮助您开始。

编辑:如果您填充组合框,您将需要每行一列,不是吗?如果没有,您需要重新present全行作为以某种方式的字符串。然后,你把在价值,并直接把值在最后数组列表,让你的循环需要看起来像这样:

 的ArrayList<串GT;人=新的ArrayList<串GT;();
    而(rs.next()){
            ArrayList的<串GT;记录=新的ArrayList<串GT;();
            的for(int i = 1; I< =列;我++){
                    字符串值= rs.String(I)
                    record.add(值);
            }
            字符串值= methodWhichConvertsArrayListToStringTheWayYouNeedItFormatted(记录);
            al.add(值);
    }
    返回人;

I am writing a program that will query an MS access database, return the query as a result set, and then I want to ultimately convert that result set into a String array, so that I can pass it into the constructor of a Swing JComboBox - so the ComboBox will list the items returned by the query.

I have been able to store the rows of the result set into an ArrayList, and then convert that ArrayList into an object array, and the combobox will list the correct items, but as objects. I simply cannot ever cast that ArrayList to a String array. Does anyone know if this is possible? Here is some of my code...

// Convert the Resultset into an array list

public ArrayList<ArrayList<Object>> Results2Array(ResultSet rs) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();

    ArrayList<ArrayList<Object>> al = new ArrayList<ArrayList<Object>>();

    while (rs.next()) {
        ArrayList<Object> record = new ArrayList<Object>();

        for (int i = 1; i <= columns; i++) {
            Object value = rs.getObject(i);
            record.add(value);
        }
        al.add(record);
    }
    return al;
}

// Convert ArrayList to Object Array, and pass into GUI

ArrayList<String> Locations = new ArrayList<String>();
ArrayList<String> Months = new ArrayList<String>();
ArrayList<String> Years = new ArrayList<String>();

try {
    DB.loadDriver();
    DB.makeConnection();
    DB.buildStatement();

    Locations = DB.getLocations();
    Months = DB.getMonths();
    Years = DB.getYears();

    Object[] arrLocations = Locations.toArray();
    Object[] arrMonths = Months.toArray();
    Object[] arrYears = Years.toArray();

    dbGUI ui = new dbGUI(arrLocations, arrMonths, arrYears);
    ui.setVisible(true);

Can anyone offer any suggestions? Thanks!


UPDATE:

Here is the stack trace that I am receiving:

java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.ArrayList.toArray(Unknown Source)
    at kidsfirstdb.Main.main(Main.java:23)

解决方案

   String[] arrLocations = locations.toArray(new String[0]);

Is the correct answer. The reason for your exception is that all the objects are in fact not strings.

You need to change this:

   Object value = rs.getObject(i);

to this:

   String value = rs.getString(i);

or this:

   String value = rs.getObject(i).toString();

That last one will need a null check if you can be returning null columns.

Note that the toString() representation may not be exactly what you are looking for in all cases, but it will get you started.

Edit: If you are filling a combo box, you are going to need one column per row, no? If not, you need to represent the whole row as a string in some fashion. Then you put that in the value and put the value directly in the final array list, so your loop needs to look like this:

    ArrayList<String> al = new ArrayList<String>();
    while (rs.next()) {
            ArrayList<String> record = new ArrayList<String>();
            for (int i = 1; i <= columns; i++) {
                    String value = rs.String(i);
                    record.add(value);
            }
            String value = methodWhichConvertsArrayListToStringTheWayYouNeedItFormatted(record);
            al.add(value);
    }        
    return al;

这篇关于将Java结果集为String数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆