如何在 ms 访问中从数据库中的 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();
}
}
组合框显示正确,但只显示数据库 table 中的一项,这是为什么?以及如何允许数据库中的 jcombobox 包含多个项目?还有为什么它总是抱怨并建议抑制警告?
不要使用空布局和 setBounds(...)。 Swing 旨在与布局管理器一起使用。
不需要 comboBox.setVisible(true)
,因为默认情况下 Swing 组件(顶级容器除外)是可见的。
Also why does it always complain with and advises suppress warnings?
您尚未指定将添加到组合框模型的数据类型。您正在添加字符串数据,因此您应该使用:
JComboBox<String> combo = new JComboBox<String>();
阅读 Generics 了解更多信息。
how do I allow multiple items to be in jcombobox from database?
你没有做任何特别的事情。您的代码看起来很合理,因为您有一个 while 循环并且调用了 addItem(...) 方法。
如果您只有一件商品,那么我猜您的查询只会 returns 一件商品。您的输出应验证这一点。向您的 table.
添加更多数据
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();
}
}
组合框显示正确,但只显示数据库 table 中的一项,这是为什么?以及如何允许数据库中的 jcombobox 包含多个项目?还有为什么它总是抱怨并建议抑制警告?
不要使用空布局和 setBounds(...)。 Swing 旨在与布局管理器一起使用。
不需要 comboBox.setVisible(true)
,因为默认情况下 Swing 组件(顶级容器除外)是可见的。
Also why does it always complain with and advises suppress warnings?
您尚未指定将添加到组合框模型的数据类型。您正在添加字符串数据,因此您应该使用:
JComboBox<String> combo = new JComboBox<String>();
阅读 Generics 了解更多信息。
how do I allow multiple items to be in jcombobox from database?
你没有做任何特别的事情。您的代码看起来很合理,因为您有一个 while 循环并且调用了 addItem(...) 方法。
如果您只有一件商品,那么我猜您的查询只会 returns 一件商品。您的输出应验证这一点。向您的 table.
添加更多数据