查询不返回输出

时间:2013-07-30 16:13:33

标签: java mysql netbeans

我正在编写一个程序,当用户使用单选按钮选择某个选项时,会分配参数和sql语句:

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();            
    }

当我运行时:

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());
    }

此处标记的声明没有输出。这是为什么?

2 个答案:

答案 0 :(得分:3)

您的陈述似乎不一定符合您的想法;

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.

此外,虽然很可能不是您的问题,getInt(1)getTimestamp(2)似乎对SELECT *有点危险。

答案 1 :(得分:0)

检查getConnection()字符串和Query / Prepared语句。

由于它不会返回任何结果,因此可能存在问题。