0

I am currently learning JDBC and can successfully query and retrieve data from a MysQL database. But I would like to run this as a method and return the resultset rows from the method to the calling program.

So far I think I have made an appropriate method but am stuck returning the resultset as an object. Eclipse gives me the following error for the return type:

Object cannot be resolved to a type

Here is the method:

public object getAllPosts(int catID) throws Exception{
    try{
        this.catID = catID;
        sql = "SELECT post_id, post_title, post_content, post_date FROM crm_posts WHERE cat_id = ? LIMIT ?";
        prep = conn.prepareStatement(sql);
        prep.setInt(1, catID);
        prep.setInt(2, 3);
        // execute statement
        rs = prep.getResultSet();

    } catch(Exception e){
        e.printStackTrace();
    }
    return rs;
}

What is the return type I should use or is there a better way of doing this?

EDIT:

As stated in the comments it was a (annoying) typo. Object with a capital O.

4
  • 1
    Is it typo object in return type. It should be Object Commented May 31, 2014 at 11:43
  • 1
    Why don't you return a ResultSet? That way calling methods can use it without typecasting. Commented May 31, 2014 at 11:45
  • Thanks @Braj, feel free to make an answer, and thanks @Frazz Ill use ResultSet. Commented May 31, 2014 at 11:53
  • Just edit it in your post as solution. Commented May 31, 2014 at 11:54

3 Answers 3

2

If you're not going to return a ResultSet or Object object, then you should define your return type. You could create your own Post object for each row in the ResultSet, put those objects in a list, and return the list.

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

Comments

1

This is how you do it

public List<MyClassThatRepresentsTableInDatabase> getAllPosts(int catID) throws Exception{
    try{
        this.catID = catID;
        sql = "SELECT post_id, post_title, post_content, post_date FROM crm_posts WHERE cat_id = ? LIMIT ?";
        prep = conn.prepareStatement(sql);
        prep.setInt(1, catID);
        prep.setInt(2, 3);
        // execute statement
        rs = prep.getResultSet();
        List<MyClassThatRepresentsTableInDatabase> ret=new ArrayList<>();
        while(rs.hasNext()){
           MyClassThatRepresentsTableInDatabase item=
                                 new MyClassThatRepresentsTableInDatabase();
           item.setId(rs.getLong("post_id");
           item.setName(rs.getString("post_title");
           item.setContentd("post_content");
           //etc..
           ret.add(item);
        }
        return ret;
    } catch(Exception e){
        e.printStackTrace();
    }
    return new ArrayList();

}

Comments

0

Excecute your query in ResultSet and loop it to get your results.

Use the following line of codes :

Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()) 
{
     out.println(rs.getString("//Your Column name"));
}

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.