try/catch jdbc java can';不要把jpanel放在jframe中
当我执行try-and-Catch函数时,我不能在我的Jframe中添加jpanel,我已经尝试在每个try-and-Catch中添加我的面板,但是没有用,请帮助我! 另外,只有在我执行try/catch jdbc java can';不要把jpanel放在jframe中,java,mysql,swing,jpanel,try-catch,Java,Mysql,Swing,Jpanel,Try Catch,当我执行try-and-Catch函数时,我不能在我的Jframe中添加jpanel,我已经尝试在每个try-and-Catch中添加我的面板,但是没有用,请帮助我! 另外,只有在我执行frame.add(新产品(“lol”))时,它才有效在try函数之前 public static void main(String[] args) { Connection conn = null; Statement stmt = null; frame.set
frame.add(新产品(“lol”))时,它才有效代码>在try函数之前
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
frame.setVisible(true);
frame.setSize (new Dimension (704, 454));
GridLayout layout = new GridLayout(3, 3, 41, 10);
String shit = null;
frame.setLayout (layout);
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
stmt= conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("select productName from product");
rs.next();
String name = rs.getString("productName");
System.out.println(name);
shit = name;
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
frame.revalidate();
frame.repaint();
frame.add(new Product("lol"));
}
}
产品类别
public Product(String name){
JLabel label1 = new JLabel(name);
add(label1);
setBorder(blackline);
}
添加组件后,您需要调用repaint()
和revalidate()
,因此您应该更改此代码
frame.revalidate();
frame.repaint();
frame.add(new Product("lol"));
对此
frame.add(new Product("lol"));
frame.revalidate();
frame.repaint();
哇,谢谢你修复了它,我现在觉得自己很愚蠢:((更好的方法是在启动时在GUI中放置一个空表,并在DB查询之后填充数据。