Dmitri S.
Dmitri S.

Reputation: 3438

Strange problem with JDBC, select returns null

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

Upvotes: 0

Views: 2648

Answers (4)

duffymo
duffymo

Reputation: 308743

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

Upvotes: 6

Andreas Petersson
Andreas Petersson

Reputation: 16518

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.

Upvotes: 2

Makach
Makach

Reputation: 7569

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.

Upvotes: 2

Hobo
Hobo

Reputation: 7611

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.

Upvotes: 3

Related Questions