Search code examples
javaswingjtablejoptionpane

How can I pass string to a JOptionPane?


I have tried to write a code that searches a database of students and shows the searched result on a option pane. And I ended up writing the following. The expected result is:

Name: "something" Roll: "something" Registration: "Something"

But actually the output is something like this:

Name: null Roll: null Registration: null


public class Search

{

JFrame sw = new JFrame("Search Students' Info");   //search window
JTable stable;
JLabel stfl = new JLabel("Roll");                  //search text field label
JTextField stf = new JTextField(8);               //search text field


JButton sb = new JButton("Search");             //search button

public void exsearch()                              //Execute Search
{
    sw.setLayout(new GridLayout(2,2));
    sw.add(stfl);
    sw.add(stf);
    sw.add(sb);


    sw.setSize(200, 100);
    sw.setLocation(100, 100);
    sw.setVisible(true);

    sb.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/srsdb";
            try
            {
                Class.forName(driver).newInstance();

                java.sql.Connection con = DriverManager.getConnection(url, "root", "");
                JOptionPane.showMessageDialog(null, "Connected", "Connection Confirmation", JOptionPane.PLAIN_MESSAGE);

                String str ="SELECT* FROM students WHERE Roll="+stf.getText();
                java.sql.Statement st = con.createStatement();
                java.sql.ResultSet rs = st.executeQuery(str);

                rs.first();

                int n = rs.getMetaData().getColumnCount();
            //  String[] columnnNames;
                String[] attributes= new String[10];

                int j;

                while(rs.next())
                {
                    for(j=0; j<3; j++)
                    {
                        attributes[j] = rs.getString(j);
                    }
                }
                    JOptionPane.showMessageDialog(null, "Name :"+attributes[0]+"\nRoll :"+attributes[1]+"\nRegistration :"+attributes[2], "Search Result", JOptionPane.PLAIN_MESSAGE);
            }
        catch(Exception f)
        {
            f.printStackTrace();
            JOptionPane.showMessageDialog(null, "Not Found", "Search Result", JOptionPane.PLAIN_MESSAGE);
        }   

    }});


}

public static void main (String[] args)
{
    new Search();
}

}


Solution

  • I would use a debugger to check which part of your code is omitted. One wierd thing I see is, that you jump to the first record in your result set:

    rs.first();
    

    And then you read the data from all subsequent records in a while loop

     while(rs.next())
     {
        for(j=0; j<3; j++)
        {
            attributes[j] = rs.getString(j);
        }
     }
    

    This ensures, that you get the data from the last matching record if there is more than one. Better would be to check if there is zero or more than one record. If that is the case, issue a warning because probably there is something wrong with your code (use a debugger to find out what). If there is only one record, read the data from that record and print it (more or less like you try with rs.getString())