我想用数据库列 (SQLite) 填充 JComboBox。
我的数据库连接是通过 anther 包中名为 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
请帮忙!
最佳答案
我不太确定你在期待什么......
statJCBaccountname
甚至不在您提供的代码示例中,但编译器表示该变量未定义DatabaseConnection
类中没有createStatement
这样的方法
您需要在程序编译之前解决这些问题。我建议不要使用 YouTube 教程,除非您认识作者。
看看JDBC Database Access了解更多详情...
关于Java - 使用 SQLite 数据库填充 JCombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978495/