选择查询,将出现在 Netbeans 和 mysql 中的组合框中
Posted
技术标签:
【中文标题】选择查询,将出现在 Netbeans 和 mysql 中的组合框中【英文标题】:Select Query and will appear to comboBox in Netbeans & mysql 【发布时间】:2014-02-26 11:27:47 【问题描述】:我的代码有问题: 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
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|状态 |charityWardID| +-------------+--------+-------------+ |......1......|..打开..|......1......| |......2......|..打开..|......1......| |......3......|..打开..|......2......| +-------------+--------+-------------+
我有 2 个组合框:cb_ward2 = which contains wardName<br>
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
我正在使用 Netbeans 和 MYSQL
编辑:
导入 java.sql.*; 导入 javax.swing.*; 公共类 addBed 扩展 javax.swing.JFrame 连接 conn = null; 结果集 rs = null; PreparedStatement pst = null; /** * 创建新表单 addBed */ 公共添加床() 初始化组件(); 无效负载组合() 尝试 字符串charitysql =“从tbl_charityward选择病房名称,charityRoomID,tbl_charityroom2 WHERE tbl_charityward.charityWardID =?”; pst = conn.prepareStatement(charitysql); pst.setInt(1,tbl_charityroom2.charityWardID); rs = pst.executeQuery(); 而(rs.next()) cb_ward2.addItem(rs.getString(1)); cb_room2.addItem(rs.getString(2)); 捕获(异常 e) JOptionPane.showMessageDialog(null, e); 私人无效formWindowOpened(java.awt.event.WindowEvent evt) conn = myconn.ConnectDb(); 加载组合(); 私人无效btn_add2ActionPerformed(java.awt.event.ActionEvent evt)
【问题讨论】:
【参考方案1】:你使用的方式不是使用 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();
【讨论】:
@Jugadu 感谢纠正错误【参考方案2】:由于您的基本要求是将数据显示为两个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);
现在您可以在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);
);
【讨论】:
我尝试了您的代码,但出现错误:“未编译的源代码 - 错误的树类型:”,我在 'pst.setString(1,tbl_charityroom2 中注意到了。 CharityWardID)' 'tbl_charityroom2' 有红色下划线。我把 tbl_charityroom 换成 tbl_charityroom2 不好。 你的表名 tbl_charityroom 或 tbl_charityroom2 是什么?pst.setString(1,tbl_charityroom2.charityWardID)
应该是 pst.setInt(1,tbl_charityroom2.charityWardID)
因为 charityWardID
是 int
看到编辑
什么也没发生。和 pst.setString 一样,加红色下划线
声明它是什么意思?对不起,我是 netbeans 的新手【参考方案3】:
尝试使用数组列表!它会给你一个类似推的效果
类似的东西
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
【讨论】:
以上是关于选择查询,将出现在 Netbeans 和 mysql 中的组合框中的主要内容,如果未能解决你的问题,请参考以下文章