我使用Netbeans设计JFrame。使用Cardlayout我在ParentPanel中创建了两个JFrame。如果按下按钮,我想在面板之间切换。
MotherPanel.removeAll();
MotherPanel.add(detailPanel);
MotherPanel.repaint();
MotherPanel.revalidate();
detail();
方法detail()访问数据库中的表并将特定值设置为Jtextfields。
public void detail(){
doConnect();
setTitle(cur_categ);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void connection() throws SQLException{
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String SQL = String.format("SELECT * FROM %s", cur_categ);
rs_det = stmt.executeQuery(SQL);
}
public void doConnect(){
try{
//Connect to the database
String host = "jdbc:derby://localhost:1527/Employees";
String userName = "App";
String pass = " ";
con = DriverManager.getConnection(host, userName, pass);
connection();
//Move the cursor to the first record and get the data if there is any
if (rs_det.next()){
double exp_dou = rs_det.getDouble("Expense");
String exp = Double.toString(exp_dou);
String desc = rs_det.getString("Description");
String date = rs_det.getString("Date");
//Display the records in the text fields
textExpense1.setText(exp);
textDescription.setText(desc);
textDate.setText(date);
}
}
catch ( SQLException err ){
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
数据库中有几个表,第二帧访问,这取决于第一个Jpanel中jtable中选择的行。 我可以打开第二个Jpanel,但是当它使用数据库中的值访问表时我无法重新打开它。但是,如果表中没有值,我可以重新打开第二个Jpanel。我该如何解决?