为什么我不能在构造函数外执行查询?我不能使用我在构造函数中声明的变量。为什么不?我必须将数据库连接放在带参数的方法中吗?我不能在构造函数外使用数据库连接,为什么?
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);
}
}
}
确定感谢信息被盗。 – Suranga
不客气 – Suranga