***How To Search Value From Data Base and IF Data is Not Available then show a message "Data Is Unavilable"

My Codes are as Follows :-***

private void Sf_Reg_search_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     

        String str2 = JOptionPane.showInputDialog(null, "Please Enter Staff Reg No: ","Enter Reg No For Search", 1);
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:HMS_DS","","");
            sql="Select * from Staff_Reg where sf_Reg_No='"+str2+"'";
            stmt=con.createStatement();
            rs=stmt.executeQuery(sql);
//            ResultSetMetaData md = rs.getMetaData(); 
//            int columns = md.getColumnCount(); 
            String a3="";
             if(str2.equals(""))
                    {
                        JOptionPane.showMessageDialog(null,"Please Enter Reg No.");
                    }
             else{

                    while(rs.next())
                    {
                     sf_rg_no_TextField.setText(rs.getString("sf_Reg_No"));
                     sf_name_TextField.setText(rs.getString("sf_Name"));
                     sf_age_CB.setSelectedItem(rs.getString("sf_Age"));

                       if(sf_male_RB.isSelected()) 
                       {
                            a3=sf_male_RB.getText();
                       } 
                       else 
                       {
                            a3=sf_female_RB.getText();
                        }
                     sf_adrs_TextArea.setText(rs.getString("sf_Adrs"));
                     sf_ph_TextField.setText(rs.getString("sf_Ph"));
                     sf_UN_TextField.setText(rs.getString("sf_UN"));
                     sf_PW_PT.setText(rs.getString("sf_PW"));
                    }
             }
        }  
       catch(Exception ex)
       {
           //JOptionPane.showMessageDialog(null,ex);
           JOptionPane.showMessageDialog(null,"Record Not Found.");
       }
}                

you can write sql, sure, you can also use a framework like hibernate.
you can check the number of results you get in your resultset, or check if it returns null.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.