选择查询,将出现在Netbeans&中的comboBox中。 MySQL的

时间:2014-02-03 04:13:07

标签: java mysql sql

我对这段代码有疑问:
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。

例如:
我选择SurgicalcharityRoomID = 1, 2 将出现在cb_room2
当我选择Obygine时,charityRoomID = 3 只会出现在cb_room2上,
但是当我选择Pediatric时,没有charityRoomID will appear on cb_room2

will appear on cb_room2


I am using Netbeans and MYSQL


EDIT :

3 个答案:

答案 0 :(得分:0)

您使用的方式不是使用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();

答案 1 :(得分:0)

由于您的基本要求是将数据显示为两个ComboBoxes,您可以这样做:

您可以使用loadcombo()cb_ward2组合框与数据库值一起加载。

void loadcombo() {
    try
    {
     Connection conn=null;
 PreparedStatement pst=null;
 ResultSet rs=null;
// Your database connections 

     String charitysql = "SELECT wardName FROM tbl_charityward";
     pst = conn.prepareStatement(charitysql);
     rs = pst.executeQuery();
    while(rs.next()){                            
        cb_ward2.addItem(rs.getString(1));
    }
    con.close();
    }
    catch(Exception e)
    {
        System.out.println("Error"+e);
    }    
}

现在,您可以ActionListener使用cb_ward2作为:

cb_ward2.addActionListener(new ActionListener(){


    public void actionPerformed(ActionEvent ae){
        try
    {
        Connection con=null;
        PreparedStatement pst=null,pst1=null;
        ResultSet rs=null,rs1=null;
    //your database connection
                Object name=cb_ward2.getSelectedItem();
                    pst=con.preparedStatement("select charityWardID from tbl_charityward where wardName=?");
                pst.setObject(1,name);
                    rs= pst.executeQuery();
        if(rs.next())
        {
                      pst1=con.preparedStatement("select charityRoomID from tbl_charityroom2 where charityWardID=?");
                      pst1.setInt(1,rs.getInt(1));
                      rs1=pst1.executeQuery();
                      while(rs1.next()){
                      cb_room2.addItem(rs1.getInt(1));
                      }
            }
                con.close();
    }
    catch(Exception e)
    {
        System.out.println("GG"+e);
    }

    }


    });

答案 2 :(得分:0)

尝试使用arrayList !!它会给你一种推动效果

类似这样的事情

    ArrayList<String> categories = new ArrayList<>();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        System.out.println("\ntrying connection");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/products?user=root&password=");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT DISTINCT categorie FROM product");
        while (rs.next()) {
            categories.add(rs.getString(1));
        }
        for (String item : categories) {
            ComboBoxCategorie.addItem(item);
        }

    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) {
            } // ignore
            stmt = null;
        }
    }

你可以忽略catch和finally子句!!只需关闭stmt