简体   繁体   English

如何在MS Access中从数据库的jcombobox添加多个项目

[英]How do I add multiple items from a jcombobox from a database in ms access

import java.sql.*;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class ComboExample {
 Connection con;
 Statement st;
 ResultSet rs;

ComboExample() {
    JFrame f = new JFrame();
    f.getContentPane().setLayout(null);
    final JComboBox combo = new JComboBox();
        try {
             String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
             Class.forName(driver);
             String db = "jdbc:odbc:TTracking";
             con = DriverManager.getConnection(db);

             // Getting database info
             DatabaseMetaData meta = con.getMetaData();
             System.out.println("Server name: " +
meta.getDatabaseProductName());
             System.out.println("Server version: " +
meta.getDatabaseProductVersion());
             System.out.println("");

             System.out.println("Creating statement...");
              st = con.createStatement();
              String sql = "SELECT Trailer FROM TrailerLocationMaster";
              ResultSet rs = st.executeQuery(sql);
                    while (rs.next()) {
        String trailer = rs.getString("Trailer");
        combo.addItem(trailer);
        System.out.println(rs.getString("Trailer"));
        combo.setVisible(true);
        }
        }
         catch (Exception ex) {
            }

    combo.setBounds(20, 50, 150, 20);
    f.add(combo);
    f.setSize(400, 200);
    f.setVisible(true);

}






public static void main(String[] args) {
    ComboExample c = new ComboExample();
    }
}

The combo-box displays correctly, but only one item from the database table appears why is that? 组合框显示正确,但是为什么只显示数据库表中的一项? and how do I allow multiple items to be in jcombobox from database? 以及如何允许多个项目从数据库进入jcombobox? Also why does it always complain with and advises suppress warnings? 另外,为什么它总是抱怨并建议禁止警告?

Don't use a null layout and setBounds(...). 不要使用null布局和setBounds(...)。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。

There is no need for comboBox.setVisible(true) since Swing components (except for top level containers) are visible by default. 由于默认情况下Swing组件(顶级容器除外)是可见的,因此不需要comboBox.setVisible(true)

Also why does it always complain with and advises suppress warnings? 另外,为什么它总是抱怨并建议禁止警告?

You haven't specified the type of data that will adding the to the model of the combo box. 您尚未指定将添加到组合框模型的数据类型。 You are adding String data so you should be using: 您正在添加字符串数据,因此您应该使用:

JComboBox<String> combo = new JComboBox<String>();

Read up on Generics for more information. 阅读有关泛型的更多信息。

how do I allow multiple items to be in jcombobox from database? 如何允许多个项目从数据库进入jcombobox?

You don't do anything special. 你没什么特别的。 Your code looks reasonable since you have a while loop and you invoked the addItem(...) method. 您的代码看起来很合理,因为您有一个while循环,并且调用了addItem(...)方法。

If you only have one item, then I would guess your query only returns a single item. 如果您只有一个项目,那么我猜您的查询只会返回一个项目。 Your output should verify this. 您的输出应对此进行验证。 Add more data to your table. 将更多数据添加到表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM