为什么我不能在构造函数之外执行查询?我不能使用我在构造函数中声明的变量。为什么不?我是否必须将数据库连接放在带参数的方法中?
public class main extends javax.swing.JFrame
{
DefaultListModel model = new DefaultListModel();
public main()
{
initComponents();
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/project", "root", "password");
Statement stat = con.createStatement();
ResultSet resultaat = stat.executeQuery(query);
while (resultaat.next())
{
model.addElement(resultaat.getString(2));
}
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}