views:

281

answers:

4

Hello everybody. I am trying to use JDBC and my query is working in some cases but not working in others. I would really appreciate any help.

Some of my code:

public Result getSpecificTopic()
    {
        String query = "Select msg_body, msg_author from lawers_topic_msg";// where msg_id=2 order by msg_id desc";
         try
        {
            con = mysql.getConnection();
            //Statement stmt = con.createStatement();
            PreparedStatement stmt = con.prepareStatement(query);
            //stmt.setInt(1, topicId);
            ResultSet rs = stmt.executeQuery(query);
            int rowCount = rs.getRow();
            specificTopic = ResultSupport.toResult(rs);

            con.close();
            stmt.close();
        }
        catch(Exception e)
        {
        }
        return this.specificTopic;
    }

    public void setTopicId(String num)
    {
        this.topicId = Integer.parseInt(num);
    }

    public int getTopicId()
    {
        return this.topicId;
    }

However if i change

String query = "Select msg_body, msg_author from lawers_topic_msg";

to the

String query = "Select msg_body, msg_author from lawers_topic_msg where msg_id = " + topicId;

Then the resultset retunrs nothing.... I am breaking my head here and still cannot figure out what is the problem

+2  A: 

As a first step, it'd be worth making sure an exception's not being thrown - at the very least log something in your catch() block.

Also be worth logging the SQL generated, and making sure that actually returns what you expect from the database when running it directly.

If you have multiple databases, it'd be worth confirming you're running against the one you think you are - I'm embarrassed to admit I've been caught out that way before.

Hobo
+1 for "do something in your catch block".
extraneon
+2  A: 

several issues with your code, i'll keep it short:

don't encapuslate with try / catch at this layer, aspecially not since you're doing no error management. this.specificTopic looks global, so if your query fails it will return whatever was stored in this.specificTopic.

also try what BobbyShaftoe said. print in console or use your debugger. This should give you a good indication on what is wrong.

Makach
topicId is just private variable in same class.But you spot on about running query directly trough database.
Dmitris
+2  A: 

my first guess would be Integer.parseInt(num) could throw an exception. if so, the sql statement will be broken.

secondly, as Makach pointed out, there are several issues. first the catch-all

you should not use string concatenation, like

      ....where msg_id = " + topicId;

but rather

      ....where msg_id = ?"
         stmt.set Int(1,topicId)

edit: it seems thats what you were trying anyways, SO sucks in some characters.

Andreas Petersson
Thanks for the advice regarding my code.I changed it now to the proper way.
Dmitris
+5  A: 

You still aren't closing your resources properly. That should be done in a finally block:

http://www.java-blog.com/correct-closing-jdbc-resources

duffymo
Thanks for the link and advice.I changed it.
Dmitris