在java中使用sql查询更新5个组合框

时间:2023-01-27 18:59:55

I have an MS Access database, in which there is a set of tablet names in column name 'tname'. I have 5 comboboxes in java applet. All the 5 comboboxes must update the items in tname, each time when it loads. I used the following code. It adds only the first item in tname to the first combobox only. Other boxes remain empty. But if I update a single combobox, it works. But I need 5 comboboxes to get updated. Kindly guide me through the problem I've made.

我有一个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();
}

3 个解决方案

#1


2  

I have tried, it works all fine as i was wondering what is the need to have a temp variable look at my code, its fully working code

我试过,它工作得很好,因为我想知道什么是需要一个临时变量看我的代码,它的完全工作的代码

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();
    }
}

it populates cboxes with all rows in table

它用表中的所有行填充cbox

#2


1  

Assuming the variables named ct[1-5]9 reference distinct instances of JComboBox, "problems may arise if you add duplicate String objects." If necessary, you can duplicate the items, as shown in the API.

假设名为ct [1-5] 9的变量引用了JComboBox的不同实例,“如果添加重复的String对象,可能会出现问题。”如有必要,您可以复制项目,如API中所示。

A better approach is to update a single DefaultComboBoxModel that is shared by each JComboBox instance.

更好的方法是更新每个JComboBox实例共享的单个DefaultComboBoxModel。

#3


0  

Instead of assigning each retrieved value for each combobox, assign it to a temp string and assign that to each combobox.

不是为每个组合框分配每个检索的值,而是将其分配给临时字符串并将其分配给每个组合框。

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

#1


2  

I have tried, it works all fine as i was wondering what is the need to have a temp variable look at my code, its fully working code

我试过,它工作得很好,因为我想知道什么是需要一个临时变量看我的代码,它的完全工作的代码

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();
    }
}

it populates cboxes with all rows in table

它用表中的所有行填充cbox

#2


1  

Assuming the variables named ct[1-5]9 reference distinct instances of JComboBox, "problems may arise if you add duplicate String objects." If necessary, you can duplicate the items, as shown in the API.

假设名为ct [1-5] 9的变量引用了JComboBox的不同实例,“如果添加重复的String对象,可能会出现问题。”如有必要,您可以复制项目,如API中所示。

A better approach is to update a single DefaultComboBoxModel that is shared by each JComboBox instance.

更好的方法是更新每个JComboBox实例共享的单个DefaultComboBoxModel。

#3


0  

Instead of assigning each retrieved value for each combobox, assign it to a temp string and assign that to each combobox.

不是为每个组合框分配每个检索的值,而是将其分配给临时字符串并将其分配给每个组合框。

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