gpt4 book ai didi

java - 选择“查询”,将出现在 Netbeans 和 mysql 中的组合框中

转载 作者:行者123 更新时间:2023-11-30 00:44:16 25 4
gpt4 key购买 nike

我在这段代码上遇到问题:
String charitysql = "SELECT wardName, charityRoomID FROM tbl_charityward,tbl_charityroom2 <br/>WHERE tbl_charityward.charityWardID = tbl_charityroom2.charityWardID";<br/>

 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<br/>
charityWardID int NOT NULL AUTO_INCREMENT,<br/>
wardName varchar(20),<br/>
status varchar(20),<br/>
PRIMARY KEY (charityWardID)

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

这是我的表格,其中包含值:
tbl_charityward<br/>
+-------------+----------+--------+<br/>
|charityWardID| wardName | status |<br/>
+-------------+----------+--------+<br/>
|......1......| Surgical |..Open..|<br/>
|......2......| .Obygine |..Open..|<br/>
|......3......| Pediatric|..Open..|<br/>
+-------------+----------+--------+<br/>



tbl_charityroom2<br/>
+-------------+--------+-------------+<br/>
|charityRoomID| status |charityWardID|<br/>
+-------------+--------+-------------+<br/>
|......1......|..Open..|......1......|<br/>
|......2......|..Open..|......1......|<br/>
|......3......|..Open..|......2......|<br/>
+-------------+--------+-------------+<br/>

我有 2 个组合框:
cb_ward2 = which contains wardName<br/> cb_room2 = which contains CharityRoomID

如果我从cb_ward2中选择一个病房名称,然后 cb_room2会显示对应的charityRoomID。

示例:
我选择SurgicalcharityRoomID = 1, 2 将出现在 cb_room2 ,
当我选择Obygine时,charityRoomID = 3 只会出现在 cb_room2 ,
但是当我选择 Pediatric ,否 charityRoomID<code> will appear on cb_room2</code>



<p>I am using <strong>Netbeans</strong> and <strong>MYSQL</strong></p>

<hr/>

<p><strong>EDIT :</strong></p>

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

关于java - 选择“查询”,将出现在 Netbeans 和 mysql 中的组合框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519541/

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