I have created a GUI based Hotel Management System in java netbeans which is connected with Ms Access Database. In database, i have a table named "RoomInfo".
I have two classes named Room and AddRoom. Room class contains all the attributes of a room and the add method to add a new room in to the database. AddRoom class has a front end form where user enters values in the JTextFields for the attributes of the room class.
When i try to add a new room in to the database, i call add_room method in Room class from AddRoom class. Problem is that when i use gettext method to get text from JTextFields, it is not reading the text from JTextFields. I have a if statement in add_room function to display a message that "None of the fields can be left empty" if any of the fields is left empty. Whenever i try to add a new room, this message pops up that "None of the fields can be left empty".
Need help identifying the problem.
Room Class
public class Room {
String roomno;
String reserved;
String category;
String airconditioned;
String bedtype;
String rent;
Connection con;
PreparedStatement ps;
ResultSet rs;
AddRoom adr = new AddRoom();
public Room()
{
roomno = "";
reserved = "";
category = "";
airconditioned = "";
bedtype = "";
rent = "";
make_connection();
}
public void make_connection()
{
try{
String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(driver);
String login = "jdbc:ucanaccess://C:\\MsDatabase\\EmployeeDB.accdb";
con = DriverManager.getConnection(login);
}catch(Exception ex){ System.out.println(ex);}
}
public void add_room()
{
try{
if("".equals(adr.get_jtextfield1().getText())||"".equals(adr.get_jtextfield2().getText())||
"".equals(adr.get_jtextfield3().getText())||"".equals(adr.get_jtextfield4().getText())||
"".equals(adr.get_jtextfield5().getText())||"".equals(adr.get_jtextfield6().getText()))
{
JOptionPane.showMessageDialog(null, "None of the fields can be left empty");
}
else
{
roomno = adr.get_jtextfield1().getText();
reserved = adr.get_jtextfield2().getText();
category = adr.get_jtextfield3().getText();
airconditioned = adr.get_jtextfield4().getText();
bedtype = adr.get_jtextfield5().getText();
rent = adr.get_jtextfield6().getText();
String sql = "INSERT INTO RoomInfo(RoomNumber,Reserved,RoomCategory,AirConditioned,BedType,RentPerDay)"
+ "VALUES(?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setInt(1, new Integer(roomno));
ps.setString(2, reserved);
ps.setString(3, category);
ps.setString(4, airconditioned);
ps.setString(5, bedtype);
ps.setInt(6, new Integer(rent));
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Room Added Successfully");
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Input in Room Number and "
+ "Rent Per Day should be a number");
}
}
}
AddRoom class
public class AddRoom extends javax.swing.JFrame {
public AddRoom() {
initComponents();
make_connection();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Room objr = new Room();
objr.add_room();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
AdminHome admh = new AdminHome();
admh.setVisible(true);
dispose();
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new AddRoom().setVisible(true);
}
});
}
public JTextField get_jtextfield1()
{
return jTextField1;
}
public JTextField get_jtextfield2()
{
return jTextField2;
}
public JTextField get_jtextfield3()
{
return jTextField3;
}
public JTextField get_jtextfield4()
{
return jTextField4;
}
public JTextField get_jtextfield5()
{
return jTextField5;
}
public JTextField get_jtextfield6()
{
return jTextField6;
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
// End of variables declaration
}
The problem is this:
AddRoom adr = new AddRoom();
This creates a new AddRoom
separate from the main window. The simplest solution is to pass it as a parameter. So delete that line and change
public void add_room()
to
public void add_room(AddRoom addr)
Then in AddRoom
:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Room objr = new Room();
objr.add_room(this);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.