我在此代码上遇到问题:
String charitysql = "SELECT wardName, charityRoomID FROM tbl_charityward,tbl_charityroom2
WHERE tbl_charityward.charityWardID = tbl_charityroom2.charityWardID";


 try {

        pst = conn.prepareStatement(charitysql);
        rs = pst.executeQuery();

            while (rs.next()) {
                String wardname = rs.getString("wardName");
                cb_ward2.addItem(wardname);
                String roomid = rs.getString("charityRoomID");
                cb_room2.addItem(roomid);

            }

        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }


这是我的表结构:
tbl_charityward
charityWardID int NOT NULL AUTO_INCREMENT,
wardName varchar(20),
status varchar(20),
PRIMARY KEY (charityWardID)


tbl_charityRoom2
charityRoomID INT NOT NULL AUTO_INCREMENT,
status varchar(20),
charityWardID int,
PRIMARY KEY (charityRoomID, charityWardID),
FOREIGN KEY (charityWardID) REFERENCES tbl_charityward (charityWardID)


这是我的值表:
tbl_charityward
+-------------+----------+--------+
|charityWardID| wardName | status |
+-------------+----------+--------+
|......1......| Surgical |..Open..|
|......2......| .Obygine |..Open..|
|......3......| Pediatric|..Open..|
+-------------+----------+--------+
tbl_charityroom2
+-------------+--------+-------------+
|charityRoomID| status |charityWardID|
+-------------+--------+-------------+
|......1......|..Open..|......1......|
|......2......|..Open..|......1......|
|......3......|..Open..|......2......|
+-------------+--------+-------------+


我有2个comboBox:
cb_ward2 = which contains wardName

cb_room2 = which contains CharityRoomID

如果我从cb_ward2中选择一个wardName,则cb_room2将显示相应的charityRoomID。

例:
我选择Surgical,并且charityRoomID = 1, 2 将出现在cb_room2上,
当我选择Obygine时,charityRoomID = 3 将仅出现在cb_room2上,
但是当我选择Pediatric时,没有charityRoomID will appear on cb_room2

I am using Netbeans and MYSQL


EDIT :

import java.sql.*;<br>
import javax.swing.*;<br>
public class addBed extends javax.swing.JFrame {

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    /**
     * Creates new form addBed
     */
    public addBed() {
        initComponents();
    }

    void loadcombo() {


            try {


            String charitysql = "SELECT wardName, charityRoomID FROM tbl_charityward, tbl_charityroom2 WHERE tbl_charityward.charityWardID = ?";
            pst = conn.prepareStatement(charitysql);
            pst.setInt(1,tbl_charityroom2.charityWardID);
            rs = pst.executeQuery();

                while (rs.next()) {

                    cb_ward2.addItem(rs.getString(1));

                    cb_room2.addItem(rs.getString(2));

                }

            }
            catch(Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }




    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) {
       conn = myconn.ConnectDb();
       loadcombo();
    }

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

    }

最佳答案

您使用的方式不是使用PreparedStatement的正确方式

像这样

String charitysql = "SELECT wardName, charityRoomID FROM tbl_charityward,tbl_charityroom2
WHERE tbl_charityward.charityWardID = ?";
pst = conn.prepareStatement(charitysql);
pst.setInt(1,tbl_charityroom2.charityWardID);
rs = pst.executeQuery();

09-12 23:12