Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

THE QUESTION : So I'm using Netbeans and derby (the database from Netbeans). There is no error in the code but the data that I

THE QUESTION :

So I'm using Netbeans and derby (the database from Netbeans). There is no error in the code but the data that I inserted did not display on the database. Ignore the comment */ thing. Please help me fix the code.

// BELOW IS THE RESERVATIONMANAGER (this one link with the database I think)

public class ReservationManager { private ArrayList reservationList; private Reservation reservation = new Reservation(); Connection con; public ReservationManager(){ } private void connectToDatabase(){ try { String host = "jdbc:derby://localhost:1527/BusTicket"; con = DriverManager.getConnection(host); } catch ( SQLException err ) { JOptionPane.showMessageDialog(null, err.getMessage()); } } public ArrayList readAll(){ //Create ArrayList object to store data from database reservationList = new ArrayList(); try { // Call method to establish database connection connectToDatabase(); // SQL statement String SQL = "SELECT * FROM Reservation"; // Declare object to execute parameterized query PreparedStatement ps = con.prepareStatement(SQL); // Execute query and returns a set of results that contains the data ResultSet rs = ps.executeQuery( ); // Loop to read each data in the rs table while(rs.next( )){ // Create new Item for each iteration reservation = new Reservation(); // Call setter to assign values to Item attributes reservation.setReservationID(rs.getString("ICNUM")); reservation.setReservationName(rs.getString("NAME")); reservation.setReservationDate(rs.getDate("DATE")); reservation.setReservationStartingFrom(rs.getString("STARTINGFROM")); reservation.setReservationDestination(rs.getString("DESTINATION")); reservation.setReservationDepartureTime(rs.getString("DEPARTURETIME")); reservationList.add(reservation); } } catch ( SQLException err ){ JOptionPane.showMessageDialog(null, err.getMessage()); } // Return the list of items to the calling method return reservationList; } public void saveReservation(Reservation reservation){ // Call getter to assign Item attributes values to the variables String icnum = reservation.getReservationID(); String name = reservation.getReservationName(); Date date = reservation.getReservationDate(); String startingfrom = reservation.getReservationStartingFrom(); String destination = reservation.getReservationDestination(); String departuretime = reservation.getReservationDepartureTime(); try { connectToDatabase(); String SQL = "INSERT INTO Reservation VALUES (?, ?, ?, ?, ?, ?)"; PreparedStatement ps = con.prepareStatement(SQL); // Set the preparestatement parameters ps.setString(1, icnum); ps.setString(2, name); ps.setDate(3, (java.sql.Date) date); ps.setString(4, startingfrom); ps.setString(5, destination); ps.setString(6, departuretime); ps.executeUpdate(); } catch (SQLException err) { JOptionPane.showMessageDialog(null, err.getMessage()); } } }

// THIS ONE IS THE MANAGERESERVATION (Where I insert data and all that)

public class ManageReservation extends javax.swing.JFrame { private ReservationManager manager = new ReservationManager(); private Reservation reservation; public ManageReservation() { initComponents();

initialBtn(); }

private void initialBtn(){ saveBtn.setEnabled(true); updateBtn.setEnabled(false); deleteBtn.setEnabled(false); resetBtn.setEnabled(false); } // Method definition: Check whether the form are completed or empty private boolean isEmptyField(){ // Check each field is empty if(nameTxtField.getText().isEmpty() || destinationBox.getSelectedIndex() == -1 || departuretimeBox.getSelectedIndex() == -1 || idTxtField.getText().isEmpty() || dateChooser1.getDate() == null || fromBox.getSelectedIndex() == -1) { // if empty, return true return true; } else { // if complete, return false return false; } } private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // Check if the form is completed if(!isEmptyField()) { // Create new item object to receive input Reservation newReservation = new Reservation(); // Call methods at class Item to set the new value from the form newReservation.setReservationName(nameTxtField.getText().trim()); newReservation.setReservationID((String)idTxtField.getText().trim()); newReservation.setReservationDate((java.sql.Date) dateChooser1.getDate()); newReservation.setReservationStartingFrom((String)fromBox.getSelectedItem()); newReservation.setReservationDestination((String)destinationBox.getSelectedItem()); newReservation.setReservationDepartureTime((String)departuretimeBox.getSelectedItem()); // Call method to save the reservation manager.saveReservation(reservation); JOptionPane.showMessageDialog(null, "The item has been saved!"); clearForm(); } // If the form is not completed yet else { // Display information message box JOptionPane.showMessageDialog(null, "Complete the form!"); } } // Call method to set initial buttons state initialBtn(); // Call method to reset the form clearForm(); // Stop the loop to find the item to be edited break; } // If do not found the selected item yet else { // Continue to read the next item object in the ArrayList continue; } } } // If the form is not completed yet else{ // Display information message box JOptionPane.showMessageDialog(null, "Complete the form!"); } */ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions