【发布时间】:2014-02-25 01:11:55
【问题描述】:
我正在关注 youtube 教程 (http://www.youtube.com/watch?v=wpbQ0DCFF0M) 以使用数据库表填充名为“comboAccountName”的 JCombobox。 我的数据库连接是在另一个类中设置的。
代码如下——
public class Execute extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
//---------------------------------------------------------------------------------------------------------------------
public Execute()
{
.............other code...............
JComboBox comboAccountName = new JComboBox();
comboAccountName.setBounds(313, 31, 302, 20);
getContentPane().add(comboAccountName);
.............other code...............
}
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));
System.out.println(rsJCBaccountname.getString(1));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Execute frame1 = new Execute();
frame1.setVisible(true);
PopulateJCB();
}
有 2 个错误需要您的帮助
comboAccountName cannot be
已解决
发生在while循环内,在下一行
comboAccountName.addItem(rsJCBaccountname.getString(1));
与
Cannot make a static reference to the non-static method PopulateJCB() from the type
执行
当我尝试调用 PopulateJCB(); 时发生在主方法中
我知道教程视频中的代码并不完全相同,但我在这里尝试做类似的事情。请帮忙。
【问题讨论】: