Java如何在Method上添加JOptionPane消息

时间:2015-07-02 15:52:57

标签: java swing joptionpane

我正在制作一个用户输入任何电影名称的GUI。如果数据库有该电影,那么它会出现在ide控制台上,如果没有则显示消息。当用户输入任何相关内容时,现在程序会在ide控制台上给出正确的输出在数据库中的电影。但问题是当输入任何错误的电影名称而不在数据库中时,程序没有做任何事情。我想在没有从数据库找到任何内容时给出JOptionPane消息像这样

enter image description here

找到数据时。

enter image description here

但我不知道该怎么做 代码:

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

主要

public class FindMain {

    public static void main(String[] args) {


        Find ooper = new Find();

    }

}

2 个答案:

答案 0 :(得分:1)

如果执行SQL查询的结果为NULL,则set.getObject(columnIndex)返回null。但在你到达那里之前,检查一下是否有任何元素。

尝试以下方法:

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

答案 1 :(得分:1)

您想检查查询是否返回空ResultSet。如果光标没有位于第一行之前,或者该集合不包含行,则方法isBeforeFirst()返回false

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
}

例如,我会提出这个改变:

...
    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){
...