【发布时间】:2014-07-16 21:35:38
【问题描述】:
我创建了一个方法,它应该从我的数据库中返回一个单元格(它是一个 varchar 变量)并将其分配给一个字符串。然后调用另一个方法,该方法采用该字符串并将变量分配给用户对象,然后将其添加到数组列表中。 SQL 查询语句在 PHPMyAdmin 中运行良好,但在 Java 中,该方法一直运行到 System.out.println("Getting Followers...");
然后由于某种原因它停止工作。我不明白,因为我已经使用这种方法的代码在程序的其他部分检索不同的变量而没有任何问题,但是每当我尝试从数据库中获取这个变量时,它都无法正常工作。
提前感谢您的帮助。
public ArrayList<User> returnFollowers(){
ArrayList<User> userArray = new ArrayList<User>();
User user = new User();
try{
Class.forName("com.mysql.jdbc.Driver");
statement = connection.createStatement();
result = statement.executeQuery("SELECT Follower FROM User_Followers WHERE Username = 'Apple123'");
if(!result.next()){
System.out.println("No results found");
}
else{
while(result.next()){
}
System.out.println("Getting followers...");
String userID = result.getString("Follower");
user.createUser(userID);
userArray.add(user);
}
}
catch(ClassNotFoundException error){
System.out.println("Error: " + error.getMessage());
}
catch(SQLException error){
System.out.println("Error " + error.getMessage());
}
finally{
if(connection != null){
try{
connection.close();
}
catch(SQLException ignore){
}
}
if(statement != null){
try{
statement.close();
}
catch(SQLException ignore){
}
}
}
return userArray;
}
【问题讨论】:
-
如果问题得到解决,然后通过接受最佳答案关闭线程。访问StackOverflow tour。