gpt4 book ai didi

java - 如何在java中使用mysql命令的条件制作按钮?

转载 作者:行者123 更新时间:2023-11-30 21:30:06 24 4
gpt4 key购买 nike

我正在申请预订电影票,然后我想通过检查数据库中的几个条件来制作一个选择座位号的按钮。我将 JButton 用于以下操作:

private void A1ActionPerformed (java.awt.event.ActionEvent evt) {
        try {
            Object day = cmbHari.getSelectedItem ();
            Object stud = cmbStud.getSelectedItem ();
            String sql = "SELECT * FROM message where id_kursi = '" + A1.getText () + "' AND id_film = '" + txtIDFilm.getText () + "' AND start = '" + txtJam.getText () + "' AND day = '"+ day +"' AND studio = '"+ stud +"' ";
            Stat statement = conn.createStatement ();
            ResultSet result = stat.executeQuery (sql);
            if (result.equals (true)) {
                JOptionPane.showMessageDialog (null, "Seat has been booked");
            } else {
                JOptionPane.showMessageDialog (null, "Seat booked");
                txtKur.setText ("A1");
            }
        } catch (SQLException ex) {
            Logger.getLogger (belitiket.class.getName ()). Log (Level.SEVERE, null, ex);
        }
    }

但即使满足所有条件,也总是可以订购座位,不应订购座位。

编辑:谢谢大家,解决了。

private void A1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
try {
Object hari = cmbHari.getSelectedItem();
String h=hari.toString();
Object stud = cmbStud.getSelectedItem();
String s=stud.toString();
String insert = "select 1 from pesan where id_kursi=? and id_film=? and mulai=? and hari=? and studio=?;";
PreparedStatement ps = conn.prepareStatement(insert);
ps.setString(1, A1.getText());
ps.setString(2, txtIDFilm.getText());
ps.setString(3, txtJam.getText());
ps.setString(4, h);
ps.setString(5, s);

ResultSet rs = ps.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "Kursi Sudah Dipesan");
txtKur.setText("");
}else{
JOptionPane.showMessageDialog(null, "Kursi Dipesan");
txtKur.setText("A1");
}
} catch (SQLException ex) {
Logger.getLogger(belitiket.class.getName()).log(Level.SEVERE, null, ex);
}
}

最佳答案

ResultSet是迭代项而不是要与之比较的 boolean 值。

此外,如果您不需要结果,请使用 SELECT 1 ... 这样,如果有一个项目,就会有一个结果,并且可以在服务器上快速完成,而不是编码不需要的信息。

关于java - 如何在java中使用mysql命令的条件制作按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56762965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com