我想用数据库列(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
请帮忙!
答案 0 :(得分:1)
我不确定你在期待什么...
statJCBaccountname
甚至不在您提供的代码示例中,但编译器说该变量未定义createStatement
类DatabaseConnection
这样的方法
您需要在程序编译之前解决这些问题。除非你了解作者,否则我建议远离YouTube教程。
请查看JDBC Database Access了解更多详情......