【发布时间】:2014-02-24 03:42:08
【问题描述】:
我想用数据库列 (SQLite) 填充 JComboBox。
我的数据库连接是通过另一个包中名为 DatabaseConnection setup 的类设置的。
这是它的样子
import java.sql.*;
import javax.swing.JOptionPane;
public class DatabaseConnection {
Connection conn = null;
public static Connection ConnectDB() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:database.db");
JOptionPane.showMessageDialog(null, "Connection Established");
conn.setAutoCommit(false);
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
在我的 JFrame 类中,我正在创建以下方法,根据 youtube 教程,该方法应该可以工作
public void PopulateJCB()
{
String queryString = "SELECT DISTINCT [Account Name] FROM main ORDER BY [Account Name]";
try
{
Connection statJCBaccountname = DatabaseConnection.ConnectDB();
Statement stmt = statJCBaccountname.createStatement();
ResultSet rsJCBaccountname = stmt.executeQuery(queryString);
while (rsJCBaccountname.next())
{
comboAccountName.addItem(rsJCBaccountname.getString(1));
}
catch (SQLException e)
{
e.printStackTrace();
}
}
但它在“comboAccountName.addItem(rsJCBaccountname.getString(1));”处显示以下错误
Multiple markers at this line
- Type safety: The method addItem(Object) belongs to the raw type JComboBox. References to generic type JComboBox<E> should be
parameterized
- comboAccountName cannot be resolved
请帮忙!
【问题讨论】:
-
无关:请学习java命名约定并遵守它们。
标签: java database swing sqlite jcombobox