【发布时间】:2015-07-02 15:52:57
【问题描述】:
我正在制作一个 GUI,用户可以在其中输入任何电影名称。如果数据库有该电影,则它会出现在 ide 控制台上,如果没有,则显示消息。现在程序在用户输入任何相关电影时在 ide 控制台上给出正确的输出在数据库中。但问题是当输入任何不在数据库中的错误电影名称时,程序不会做任何事情。当从数据库中找不到任何东西时,我想给一个 JOptionPane 消息就像这样
当找到数据时。
但我不知道该怎么做。
代码:
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();
}
}
【问题讨论】:
-
您的问题到底是什么?此外,更好地格式化您的代码将吸引更多帮助
-
@BoDidely 现在我更正了我的问题
标签: java swing joptionpane