将SQL Query结果转换为字符串数组 [英] Convert SQL Query result into array of strings
问题描述
我使用SQLiteJDBC(SQLite的Java JDBC驱动程序)在Java中查询SQLite3数据库。
如果我使SQL查询 SELECT name,传递FROM loginTable WHERE name ='%s'; 是否有返回或转换名称的函数&将查询返回的字符串传递给String数组或ArrayList?
以下代码尝试将查询返回到数组列表但它失败了,I知道SQL数据库很好&我做的查询,因为当我在命令行中执行此操作时,它可以工作。
ArrayList< String> result = new ArrayList< String>();
ResultSet rs = stat.executeQuery(SELECT ...);
for(int i = 0; rs.next(); i ++)
{
result.add(rs.getString(i));
}
它失败,因为表loginTable有列:Index,name,pass。所以看上面,当i = 0时,&我去rs.getString(0),它试图获取Index列中的字符串。
错误输出
java.sql.SQLException:第0列输出of bounds [1,1]
有谁知道如何将所有结果都放在SQL查询中(ResultSet对象)在ArrayList或数组中?
在JDBC中,列从1开始索引,而不是0。 / p>
I am making queries to a SQLite3 database in Java using the SQLiteJDBC (Java JDBC driver for SQLite).
If I make the SQL query SELECT name, pass FROM loginTable WHERE name='%s'; is there a function to Return OR Convert the name & pass strings returned from the query into a String array or ArrayList?
The following code attempts to put the queries return into an array list but it fails, I know that the SQL database is fine & the query I make because when I do this in command line it works.
ArrayList <String> result = new ArrayList<String>();
ResultSet rs = stat.executeQuery( "SELECT ..." );
for (int i=0; rs.next(); i++)
{
result.add( rs.getString(i) );
}
It fails because the table loginTable has the columns: Index, name, pass. So looking at above, when i=0, & I go rs.getString(0), it tries to get the string in the Index column.
The error output is
java.sql.SQLException: column 0 out of bounds [1,1]
Does anyone know how I can put all results from an SQL query(ResultSet object) in an ArrayList or array?
In JDBC, the columns are indexed starting from 1, not 0.
这篇关于将SQL Query结果转换为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!