从 MySql 数据库检索 JCombobox 值
我使用以下代码从我的 Sql DB 检索数据。从数据库中检索 StudName 和 StudId。StudName 将显示 JComboBox。但是如何在 Messagebox 中显示 StudId。如果我选择组合框中的第一条记录意味着我需要相应的消息框中的 ID。如何执行此操作? 提前致谢
public class FrmeA extends javax.swing.JFrame {
FrmA1 f1=new FrmA1();
Statement TmpFlxTSt=null;
ResultSet TmpFlxTRs=null;
GContnStr GCS=new GContnStr();
public FrmeA() {
initComponents();
}
public void combo(){
try{
GCS.GContnStr();
TmpFlxTSt= GCS.GCotnStr.createStatement();
String select = "Select StudId,StudName from studentmaster";
TmpFlxTRs = TmpFlxTSt.executeQuery(select);
while(TmpFlxTRs.next()){
cbx.addItem(TmpFlxTRs.getString("StudName"));
Object comboitem=cbx.getSelectedItem();
}
TmpFlxTRs.close();
TmpFlxTSt.close();
}
catch(Exception e){
System.out.println(e);
}
}
I use the following code for retrieve the data from my Sql DB.retrieve StudName and StudId from Data Base.StudName will display JComboBox.But how to show the StudId in Messagebox.If I select the first record in combo box means i need the corresponding Id in Message box.how to do this?
thanks in Advance
public class FrmeA extends javax.swing.JFrame {
FrmA1 f1=new FrmA1();
Statement TmpFlxTSt=null;
ResultSet TmpFlxTRs=null;
GContnStr GCS=new GContnStr();
public FrmeA() {
initComponents();
}
public void combo(){
try{
GCS.GContnStr();
TmpFlxTSt= GCS.GCotnStr.createStatement();
String select = "Select StudId,StudName from studentmaster";
TmpFlxTRs = TmpFlxTSt.executeQuery(select);
while(TmpFlxTRs.next()){
cbx.addItem(TmpFlxTRs.getString("StudName"));
Object comboitem=cbx.getSelectedItem();
}
TmpFlxTRs.close();
TmpFlxTSt.close();
}
catch(Exception e){
System.out.println(e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 JOptionPane 来达到此目的,类似这样的东西就可以了,
为此配置一个动作监听器或者一个 ItenStateChanged 监听器会更好。
You can use a JOptionPane for this purpose, something like this would serve,
Configure an actionlistener for this purpose or an ItenStateChanged listener would be better.
创建一个
Student
类,其中包含 ID 和名称,以及返回名称的toString()
方法,并将Student
实例添加到组合框而不是添加名称。getSelectedItem()
方法将返回一个Student
,您只需调用getId()
即可获取其 ID。Create a
Student
class, with an ID and a name, and atoString()
method which returns the name, and add instances ofStudent
to the combo box instead of adding the names. ThegetSelectedItem()
method will return aStudent
, and you'll just have to callgetId()
to get its ID.