虽然表中有很多行,但JDBC ResultSet只提供一行? [英] JDBC ResultSet is giving only one row although there are many rows in table?
本文介绍了虽然表中有很多行,但JDBC ResultSet只提供一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表中有很多行,我在我的数据库上运行相同的查询,即MySql,但java ResultSet只给出表的第一行。这是我的代码。
I am having many rows in table and I ran the same query on my database which is MySql but java ResultSet is only giving the first row of the table. Here is my code.
public ArrayList<String> getAllAlbumsName(Integer uid) {
ArrayList<String>allAlbumsName = new ArrayList<String>();
try {
String qstring = "SELECT albumname FROM picvik_picture_album WHERE " +
"uid = '" + uid + "';";
System.out.println(qstring);
connection = com.picvik.util.MySqlConnection.getInstance().getConnection();
ptmt = connection.prepareStatement(qstring);
resultSet = ptmt.executeQuery();
if(resultSet.next()) {
System.out.println(resultSet.getString("albumname"));
allAlbumsName.add(resultSet.getString("albumname"));
}
resultSet.close();
ptmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return allAlbumsName;
}
推荐答案
if(resultSet.next()) {
System.out.println(resultSet.getString("albumname"));
allAlbumsName.add(resultSet.getString("albumname"));
}
如果你想获得所有行,它应该是:
If you would like to get all rows, it should be:
while(resultSet.next()) {
System.out.println(resultSet.getString("albumname"));
allAlbumsName.add(resultSet.getString("albumname"));
}
while 语句在特定条件为真时连续执行语句块
The while statement continually executes a block of statements while a particular condition is true
注意:正如@BalusC评论的那样,你的代码会引入SQL注入攻击,最好使用ptmt.set ...而不是手动构建SQL String。
Note: As @BalusC commented, your code would introduce SQL Injection attack, it is better to use ptmt.set... Instead of constructing SQL String manually.
这篇关于虽然表中有很多行,但JDBC ResultSet只提供一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文