从 MySql 数据库检索 JCombobox 值

发布于 2024-12-23 12:35:12 字数 1125 浏览 2 评论 0原文

我使用以下代码从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2024-12-30 12:35:12

从数据库中检索StudName和StudId。StudName将显示JComboBox。但是如何在Messagebox中显示StudId。

您可以使用 JOptionPane 来达到此目的,类似这样的东西就可以了,

String message = TmpFlxTRs.getString("StudId");
JOptionPane.showMessageDialog(parent, message);

如果我选择组合框中的第一条记录,意味着我需要消息框中相应的 ID。如何做到这一点?

为此配置一个动作监听器或者一个 ItenStateChanged 监听器会更好。

retrieve StudName and StudId from Data Base.StudName will display JComboBox.But how to show the StudId in Messagebox.

You can use a JOptionPane for this purpose, something like this would serve,

String message = TmpFlxTRs.getString("StudId");
JOptionPane.showMessageDialog(parent, message);

If I select the first record in combo box means i need the corresponding Id in Message box.how to do this?

Configure an actionlistener for this purpose or an ItenStateChanged listener would be better.

停顿的约定 2024-12-30 12:35:12

创建一个 Student 类,其中包含 ID 和名称,以及返回名称的 toString() 方法,并将 Student 实例添加到组合框而不是添加名称。 getSelectedItem() 方法将返回一个 Student,您只需调用 getId() 即可获取其 ID。

Create a Student class, with an ID and a name, and a toString() method which returns the name, and add instances of Student to the combo box instead of adding the names. The getSelectedItem() method will return a Student, and you'll just have to call getId() to get its ID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文