hamel123
hamel123

Reputation: 304

Java How to add JOptionPane Messages on Method

I'm making a GUI in which user enter any movie name.If the database has that movie then it appear on ide console if not then show message.Now program give correct output on ide console when user enter any related movie which is in database.But the problem is when enter any wrong name of movie which is not in database then program doesn't do any thing.I want to give a JOptionPane message when nothing found from database just like this

enter image description here

And when data found.

enter image description here

But i don't know how to do this.
Code:

public class Find extends JFrame{

    private JLabel keyword;
    private JTextField tx;
    private JComboBox box;

    private  Connection conn;
    private PreparedStatement pre;
    private ResultSet set;
    private ResultSetMetaData meta;

    String server = "jdbc:mysql://localhost/demo";
    String user="root";
    String pass="pass";



    public Find(){
        super("Frame");
        getContentPane().setLayout(null);


        keyword= new JLabel("Keyword");
        keyword.setBounds(170, 100, 96, 37);
        getContentPane().add(keyword);


        box = new JComboBox();
        box.setModel(new DefaultComboBoxModel(new String[] {"Title"}));
        box.setBounds(42, 139, 63, 20);
        getContentPane().add(box);


        //textfield
                tx= new JTextField();
                tx.addKeyListener(new KeyAdapter() {
                    public void keyReleased(KeyEvent arg0) {


                    try{

                        conn=DriverManager.getConnection(server,user,pass);

            String option = (String) box.getSelectedItem();
            String query = "select title,year from movies where "+option+"=?";
                             //statement
                pre     =  conn.prepareStatement(query);                    


                pre.setString(1, tx.getText());



                set=    pre.executeQuery();



                ResultSetMetaData meta = set.getMetaData();


                int totalcolumn=meta.getColumnCount();



                while(set.next()){

                      for(int i=1;i<=totalcolumn;i++){

                          System.out.printf("%-8s\t", set.getObject(i));


                      }

                      System.out.println();           

                  }

                    }
                    catch(Exception e1){

                        JOptionPane.showMessageDialog(null, e1.getMessage());
                    }



                    }
                });
                tx.setBounds(120, 136, 202, 27);
                getContentPane().add(tx);




        setSize(450,450);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);   
    }
}

Main

public class FindMain {

    public static void main(String[] args) {


        Find ooper = new Find();

    }

}

Upvotes: 2

Views: 872

Answers (2)

Andrei
Andrei

Reputation: 7607

set.getObject(columnIndex) returns null if the result of executing the SQL query is NULL. But before you get there, check to see if set has any elements.

Try the following:

 ResultSetMetaData meta = set.getMetaData();

 int totalcolumn = meta.getColumnCount();

 if (!set.next() ) {
       JOptionPane.showMessageDialog(null, "No data");
 } else {
       do {
          for(int i = 1; i <= totalcolumn; i++) {
               System.out.printf("%-8s\t", set.getObject(i));
          }

          System.out.println();
      } while(set.next());
 }

Upvotes: 1

BoDidely
BoDidely

Reputation: 504

You want to check if your query returns an empty ResultSet. The method isBeforeFirst() returns false if the cursor isn't positioned before the first row or if the set contains no rows

if (!set.isBeforeFirst()) {    
    //Here you can display your 'failure' JOptionPane 
} 
else {
    //Here you can put your code that handles your set, including your 'success' JOptionPane
}

For example I would propose this change:

...
    set = pre.executeQuery();
    if (!set.isBeforeFirst()){
        JOptionPane.showMessageDialog(null, "The record was not found.");
    }
    else{
        ResultSetMetaData meta = set.getMetaData();
        int totalcolumn=meta.getColumnCount();
        while(set.next()){
            for(int i=1;i<=totalcolumn;i++){
                System.out.printf("%-8s\t", set.getObject(i));
            }
            System.out.println();           
        }
    }
}
catch(Exception e1){
...

Upvotes: 1

Related Questions