0

I'm writing a program where when user selects a certain choice using radio buttons, then parameters and sql statements get assigned:

if(r1.isSelected())//SNo
    {
        cmd="SELECT * FROM CALLDETAILS WHERE SNo=?";
        parameter=tf1.getText();

    }
    else if(r2.isSelected())//Month-wise
    {
        cmd="SELECT SNO, DATE, COMPANY,STATUS FROM CALLDETAILS WHERE MONTH(DATE)=?";
        parameter=tf1.getText();
    }
    else if(r2.isSelected())//Username
    {
        cmd="SELECT * FROM CALLDETAILS WHERE ATTENDED_BY=?";
        parameter=tf1.getText();
    }
    else if(r3.isSelected())//Company
    {
       cmd="SELECT * FROM CALLDETAILS WHERE COMPANY=?";
        parameter=tf1.getText();
    }
    else//Status
    {
        cmd="SELECT * FROM CALLDETAILS WHERE Status=?";//Resolved. in process, pending
        parameter=tf1.getText();            
    }

When I run this:

try
    {

        Connection con=DriverManager.getConnection("jdbc:mysql://localhost/tenderdetails?user=root&password=");
        PreparedStatement ps;
        ps=con.prepareCall(cmd);
        ps.setString(1,parameter);

        ResultSet rs=ps.executeQuery();       

        while(rs.next())
        {
            int sno=rs.getInt(1);
            Timestamp dt=rs.getTimestamp(2);           
            System.out.println(sno+ "  "+dt);//HERE!
        }

        con.close();
        System.out.println("here too");
    }

    catch(SQLException se)
    {
        JOptionPane.showMessageDialog(this, "Error: "+se.getMessage());
    }

There is no output of the statement marked HERE. Why is that?

10
  • 3
    Because your SQL statement did not return any hits... Commented Jul 30, 2013 at 16:15
  • 2
    It seems your query not returning any thing (while loop not executing at all). Commented Jul 30, 2013 at 16:15
  • Did you get an exception thrown? Commented Jul 30, 2013 at 16:17
  • But my database is returning values otherwise... in the mysql server. Commented Jul 30, 2013 at 16:17
  • 1
    Great, thanks! The getConnection thing solved it! xD Commented Jul 30, 2013 at 16:22

2 Answers 2

3

It would seem that your statements aren't necessarily set to what you think;

else if(r2.isSelected())//Month-wise   // <-- r2
{
    cmd="SELECT SNO, DATE, COMPANY,STATUS FROM CALLDETAILS WHERE MONTH(DATE)=?";
    parameter=tf1.getText();
}
else if(r2.isSelected())//Username     // <-- r2 again...? Won't ever be hit.

Also, although most likely not your problem, getInt(1) and getTimestamp(2) seem a bit dangerous with SELECT *.

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

1 Comment

I intend to change the SELECT *. That was just to get the code running.
0

check the getConnection() string and your Query/Prepared statement.

Since it doesn't return any results the problem is likely here.

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.