我在Netbeans中创建了2个JFrame(Booking,ConfirmBooking),两个框架都连接到SQL数据库。在Booking类中,用户通过在jtextfield中输入数字来选择显示的表格。如果用户按下下一个按钮。将处理课程预订并打开ConfirmBooking。我想访问在ConfirmBooking的jtextfield中在Booking中输入的变量,以访问用户预订的度假村的名称。
预订类编码,我想访问类中上面设置为public int的ID。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String x = jTextFieldID.getText();
ID = Integer.valueOf(x);
String sql = "SELECT RESORT_NAME FROM LouwDataBase.Resorts WHERE ID = "+ ID;
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
String resortName = rs.getString("RESORT_NAME");
int n = JOptionPane.showOptionDialog(null,
"Do you want to make a booking at "+resortName+"?",
"Confirm booking?",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Yes I do", "No I don't"},"default");
if (n == JOptionPane.YES_OPTION)
{
ConfirmBooking CB = new ConfirmBooking();
dispose();
CB.setLocationRelativeTo(null);
CB.setVisible(true);
}
else if (n == JOptionPane.NO_OPTION)
{
}
}
}
}
catch (SQLException ex)
{
Logger.getLogger(Booking.class.getName()).log(Level.SEVERE, null, ex);
}
}
ConfirmBooking类代码
public void SetUp()
{
UserList();
String sql = "SELECT RESORT_NAME, COST_PER_NIGHT_ZAR FROM LouwDataBase.Resorts WHERE ID = "+ ID;
try (PreparedStatement pstmt = conn.prepareStatement(sql))
{
try (ResultSet rs = pstmt.executeQuery())
{
if (rs.next())
{
String Name1 = rs.getString("RESORT_NAME");
double Price = rs.getDouble("COST_PER_NIGHT_ZAR");
String Rands = Double.toString(Price);
ResortName.setText(Name1);
ResortPrice.setText("R "+Rands);
}
}
}
catch (SQLException ex)
{
Logger.getLogger(Booking.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:1)
你可以修改一下ConfirmBooking类,这样你就可以在类的构造函数中传递该变量
然后你可以这样做:
if (n == JOptionPane.YES_OPTION) {
ConfirmBooking CB = new ConfirmBooking(ID);
dispose();
CB.setLocationRelativeTo(null);
CB.setVisible(true);
}
您将该ID存储在ConfirmBooking
的成员中,并在您需要时在setUp方法中检索该ID!
答案 1 :(得分:0)
您可以尝试这样的事情:
ConfirmBooking CB = new ConfirmBooking();
CB.setResortNumber(resortName);