[英]Select Query and will appear to comboBox in Netbeans & mysql
I got problem on this code:我在这段代码上遇到了问题:
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);
}
this is my tables structure:这是我的表结构:
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)
this is my tables with values:这是我的值表:
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......|
+-------------+--------+-------------+
I have 2 comboBox:我有 2 个组合框:
cb_ward2 = which contains wardName
cb_room2 = which contains CharityRoomID
If I select a wardName from cb_ward2
, then the cb_room2
will show the corresponding charityRoomID.如果我从
cb_ward2
选择一个 wardName ,那么cb_room2
将显示相应的charityRoomID。
example:例子:
I select Surgical
and the charityRoomID = 1, 2
will appear on cb_room2
,我选择
Surgical
并且charityRoomID = 1, 2
将出现在cb_room2
,
and when i select Obygine
, the charityRoomID = 3
will only appear on cb_room2
,当我选择
Obygine
, charityRoomID = 3
只会出现在cb_room2
,
but when i select Pediatric
, no charityRoomID
will appear on cb_room2
但是当我选择
Pediatric
, charityRoomID
will appear on cb_room2
I am using Netbeans and MYSQL EDIT :
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) { }
The way you are using is not the proper way of using PreparedStatement您使用的方式不是使用 PreparedStatement 的正确方式
Do like this这样做
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();
As your basic requirement is to show data into two ComboBoxes
, you can do it as:由于您的基本要求是将数据显示到两个
ComboBoxes
,您可以这样做:
You can use a loadcombo()
to load your cb_ward2
combobox with database values.您可以使用
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);
}
}
Now you can use ActionListener
on cb_ward2
as:现在您可以在
cb_ward2
上使用ActionListener
作为:
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);
}
}
});
try using an arrayList !!尝试使用 arrayList !! it will give you a push like effect
它会给你一个推动般的效果
something like this像这样的东西
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;
}
}
you can ignore the catch and finally clause!!你可以忽略 catch 和 finally 子句!! just close the stmt
只需关闭 stmt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.