将结果集的行存储在字符串数组中(Store rows of resultset in array of strings)

编程入门 行业动态 更新时间:2024-10-17 17:19:34
将结果集的行存储在字符串数组中(Store rows of resultset in array of strings)

我想计算结果集中的条目数,然后将这些值存储在数组中并传递此数组以创建图形。

ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+" as call from tablename"); // this statement will select the unique entries in a particular column provided by jtextfield int count=0; while(rs.next()) { ++count; } // This will count the number of entries in the result set.

现在我想将结果集的值存储在一个字符串数组中。 我使用了以下代码

String[] row = new String[count]; while(rs.next()) { for (int i=0; i <columnCount ; i++) { row[i] = rs.getString(i + 1); } }

错误:描述符索引无效。 请建议如何在数组中复制resultset的结果。

例如,如果我在jTextField中输入优先级,则结果集将包含priority1 priority2 priority3

I want to count the numbers of entries in resultset and then store these values in an array and pass this array to create a graph.

ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+" as call from tablename"); // this statement will select the unique entries in a particular column provided by jtextfield int count=0; while(rs.next()) { ++count; } // This will count the number of entries in the result set.

Now I want to store the values of result set in an array of string. I used the following code

String[] row = new String[count]; while(rs.next()) { for (int i=0; i <columnCount ; i++) { row[i] = rs.getString(i + 1); } }

Error : Invalid Descriptor Index. Please suggest how to copy the result of resultset in array.

For example if I enter priority in jTextField , the result set will contain priority1 priority2 priority3

最满意答案

在第一个while循环中,您将读取ResultSet中的所有条目,因此在执行第二个while循环时,没有其他内容可供阅读。 此外, ResultSet#getXxx的索引从1开始,而不是从0开始。此外,由于您不知道将要读取的行数,因此最好使用由ArrayList支持的List 。

考虑到这些,您的代码应如下所示:

ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+ " as call from tablename"); List<String> results = new ArrayList<String>(); while(rs.next()) { results.add(rs.getString(1)); }

根据您的评论,我扩展了样本:

public List<String> yourRandomQuery(String columnName) { Connection con = null; ResultSet rs = null; List<String> results = new ArrayList<String>(); try { String baseQuery = "SELECT DISTINCT %s AS call FROM tablename"; con = ...; //retrieve your connection ResultSet rs = stmt.executeQuery(String.format(baseQuery, columnName)); while(rs.next()) { results.add(rs.getString(1)); } } catch (SQLException e) { //handle your exception e.printStacktrace(System.out); } finally { closeResource(rs); closeResource(con); } return results; } //both Connection and ResultSet interfaces extends from AutoCloseable interface public void closeResource(AutoCloseable ac) { try { if (ac != null) { ac.close(); } } catch (Exception e) { //handle this exception as well... } } public void someMethod() { //retrieve the results from database List<String> results = yourRandomQuery(jTextField.getText()); //consume the results as you wish //basic example: printing them in the console for(String result : results) { System.out.println(result); } }

In your first while loop you read all the entries in the ResultSet, so when executing the second while loop there's nothing else to read. Also, the index of ResultSet#getXxx starts at 1, not at 0. Also, since you don't know the amount of rows that you will read, it will be better using a List backed by ArrayList instead.

Considering these, your code should look like:

ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+ " as call from tablename"); List<String> results = new ArrayList<String>(); while(rs.next()) { results.add(rs.getString(1)); }

Based in your comment, I extended the sample:

public List<String> yourRandomQuery(String columnName) { Connection con = null; ResultSet rs = null; List<String> results = new ArrayList<String>(); try { String baseQuery = "SELECT DISTINCT %s AS call FROM tablename"; con = ...; //retrieve your connection ResultSet rs = stmt.executeQuery(String.format(baseQuery, columnName)); while(rs.next()) { results.add(rs.getString(1)); } } catch (SQLException e) { //handle your exception e.printStacktrace(System.out); } finally { closeResource(rs); closeResource(con); } return results; } //both Connection and ResultSet interfaces extends from AutoCloseable interface public void closeResource(AutoCloseable ac) { try { if (ac != null) { ac.close(); } } catch (Exception e) { //handle this exception as well... } } public void someMethod() { //retrieve the results from database List<String> results = yourRandomQuery(jTextField.getText()); //consume the results as you wish //basic example: printing them in the console for(String result : results) { System.out.println(result); } }

更多推荐

本文发布于:2023-08-01 15:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   组中   Store   rows   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!