0

I have difficult storing multiple result sets into an ArrayList. Also I need to display them in a JTextArea. But the problem is, I can't get it to display anything.

public static ArrayList<Wall> getWallPosts(int postId){

    ArrayList<Wall> postList = new ArrayList<Wall>();
    ResultSet rs = null;
    DBController db = new DBController();

    db.setUp("myDatabase");
    String dbQuery = "SELECT Id FROM Wall WHERE ID ="+postId; 

    rs = db.readRequest(dbQuery);

    try{
       while (rs.next()){
            int wallId = rs.getInt("Id");
            String wallPoster = rs.getString("Poster");
            String wallPost = rs.getString("Post");
            String wallDate = rs.getString("Tdate");
            Wall w1 = new Wall(wallPoster, wallPost , wallDate);
            postList.add(w1);   
       }
    }
    catch (Exception e) {
       e.printStackTrace();
    }


    db.terminate();
    return postList;

}
    //The method for displaying the result sets
        public void retrieveCategoryQuestionList() {
    //get the list and store in array
    ArrayList<Wall> aList = Wall.getWallPosts(id);
    for(id = 1; id<aList.size();id++)
        jTextAreaWall.append(w2.getPost());
}

The fields in the database is Id, Poster, Post, Tdate

2
  • 1
    For future reference, bear in mind that your question doesn't exactly say what is wrong - you just say "I can't get it to display anything", and post a chunk of code. (This is particularly bad because in this situation you must have been getting an exception, so listing that and the stacktrace would have helped a lot). Further reading. Commented Jan 21, 2011 at 16:22
  • Ok, the problem is when I run the method below, it just runs DB Query: SELECT * FROM Wall WHERE ID =0. I have changed the codes according to what they said. Commented Jan 21, 2011 at 16:48

2 Answers 2

5

Here's your SQL query:

"SELECT Id FROM Wall WHERE ID ="+postId

You're selecting only the Id from the table while you expect Id, Poster, Post and Tdate to be present as per your ResultSet#getString() calls.

String wallPoster = rs.getString("Poster");
String wallPost = rs.getString("Post");
String wallDate = rs.getString("Tdate");
Wall w1 = new Wall(wallPoster, wallPost , wallDate);

You need to specify those columns in your SQL query as well:

"SELECT Id, Poster, Post, Tdate FROM Wall WHERE Id="+ postId

See also:


Unrelated to the concrete problem, I'd suggest to familarize yourself with PreparedStatement since that's the one which prevents SQL injection attacks.


Update: as per the comment on your own question, if your intent is to display ALL rows, then you should just not add a restrictive WHERE clause, even not with a Id=0, that would only search for records matching Id=0. Just omit the WHERE.

"SELECT Id, Poster, Post, Tdate FROM Wall"

Once again, I recommend to get yourself through a basic SQL tutorial. This issue has in essence not much to do with Java/JDBC. Aside from a few serious design issues, it should just work.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, the exception "handling" via e.printStackTrace(); is really bad and will obscure any problems such as this on. DB-accessing methods should always have an unambiguous way of signalling failure (ideally propagating some exception unless used in a very limited scope)!
@Andrzej: agreed. And then I'm not talking about the not closing of resources in finally. To the OP: you may find this article useful then to get new insights.
0

I don't know about other errors that could be, it looks ok to me, just that

String dbQuery = "SELECT Id FROM Wall WHERE ID ="+postId;

Should be:

String dbQuery = "SELECT Id FROM Wall WHERE ID ='"+postId+"'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.