我想计算结果集中的条目数,然后将这些值存储在数组中并传递此数组以创建图形。
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); } }更多推荐
发布评论