我有JFrame form
要求用户插入例如他的名字,然后它会显示他拥有的所有房屋的地址(用户可能拥有多个房屋)。
然而,由于我不知道查询会产生多少结果,我创建了一个vector类型的函数并将数据存储到其中,然后返回向量。在JFrame表单中,我正在做这样的事情。
Vector<String> data = User.getHouse(userName.getText());
if (data.isEmpty()) {
JOptionPane.showMessageDialog(null, "No Houses were found!");
}
else {
for(int i=0; i<data.size(); i++)
{
System.out.println(data.elementAt(i));
houses.setText(data.elementAt(i));
houses2.setText(data.elementAt(i+1));
}
}
功能代码:
public static Vector<String> getHouse(String playerName) throws SQLException, ClassNotFoundException {
Connection con = DBConnect.getConnection();
PreparedStatement getId = con.prepareStatement("SELECT Player_Id from PLAYERS WHERE Name = ?");
getId.setString(1, playerName);
ResultSet y = getId.executeQuery();
if(y.next())
{
id = y.getString("Player_id");
System.out.println("playerID => " + id);
}
System.out.println("playerName => " + playerName);
PreparedStatement s = con.prepareStatement("SELECT House_Name FROM HOUSES WHERE Player_Id = ?");
s.setString(1, id);
System.out.println("getHouse => Query");
ResultSet x = s.executeQuery();
Vector<String> data = new Vector<String>();
while(x.next())
{
data.addElement(x.getString("House_Name"));
}
return data;
}
(查询返回2行)。如何使用查询结果正确设置JLabel text
?