我有一个MS Access数据库,其中在列名“ tname”中有一组平板电脑名称。我在Java applet中有5个组合框。每次加载时,所有5个组合框都必须更新tname中的项目。我用下面的代码。它将仅将tname中的第一项添加到第一个组合框。其他框保持为空。但是,如果我更新单个组合框,它将起作用。但是我需要5个组合框才能更新。请引导我解决我所遇到的问题。

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn9=DriverManager.getConnection("jdbc:odbc:vasantham","","");
Statement st9=conn9.createStatement();
ResultSet rs9=st9.executeQuery("select DISTINCT tname from try");

while(rs9.next())
{
  ct19.addItem(rs9.getString("tname"));
  ct29.addItem(rs9.getString("tname"));
  ct39.addItem(rs9.getString("tname"));
  ct49.addItem(rs9.getString("tname"));
  ct59.addItem(rs9.getString("tname"));
}


conn9.close();
}

最佳答案

我试过了,一切正常,因为我想知道在我的代码中有一个temp变量是什么,它的代码可以正常工作

import java.awt.FlowLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ResultSetDemo {

    public ResultSetDemo() {
        JFrame f = new JFrame();
        f.setLayout(new FlowLayout());
        JComboBox cb1 = new JComboBox();
        f.add(cb1);
        JComboBox cb2 = new JComboBox();
        f.add(cb2);
        JComboBox cb3 = new JComboBox();
        f.add(cb3);
        JComboBox cb4 = new JComboBox();
        f.add(cb4);
        JComboBox cb5 = new JComboBox();
        f.add(cb5);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", my_sql_username, mysql_password);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select DISTINCT tname from try");
            while (rs.next()) {
                cb1.addItem(rs.getString("tname"));
                cb2.addItem(rs.getString("tname"));
                cb3.addItem(rs.getString("tname"));
                cb4.addItem(rs.getString("tname"));
                cb5.addItem(rs.getString("tname"));

            }
        } catch (Exception ex) {
            System.out.println(ex);
        }

        f.setSize(600, 100);
        f.setVisible(true);

    }

    public static void main(String[] args) {
        new ResultSetDemo();
    }
}


它用表中的所有行填充cbox

09-30 15:35