简体   繁体   English

Java 如何在方法上添加 JOptionPane 消息

[英]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我正在制作一个 GUI,用户可以在其中输入任何电影名称。如果数据库中有该电影,则它会出现在 ide 控制台上,如果没有,则显示消息。现在,当用户输入数据库中的任何相关电影时,程序会在 ide 控制台上提供正确的输出。但问题是进入电影中的任何错误的名称这是不是在数据库中,然后程序没有做任何的事儿。想给时JOptionPane消息时,没有从数据库中找到的就是这样的

在此处输入图片说明

And when data found.当数据找到时。

在此处输入图片说明

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

    }

}

set.getObject(columnIndex) returns null if the result of executing the SQL query is NULL.如果执行 SQL 查询的结果为 NULL,则 set.getObject(columnIndex) 返回 null。 But before you get there, check to see if set has any elements.但是在你到达那里之前,检查一下 set 是否有任何元素。

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

You want to check if your query returns an empty ResultSet.您想检查您的查询是否返回空的 ResultSet。 The method isBeforeFirst() returns false if the cursor isn't positioned before the first row or if the set contains no rows如果游标未定位在第一行之前或者集合不包含任何行,则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
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM