Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to auto refresh JTable after adding row data from MySQL database in Java Swing?

 


GUI
package Trial;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.toedter.calendar.JDateChooser;

import Libs.BookingJDBC;
import Libs.BookingLibs;
import Libs.BookingLibs2;
import Libs.JDBCBooking;

public class Demo {

	JTable table1;
	DefaultTableModel model;
	ArrayList a1;
	Object[] columnsName;
	JTextField customeridtxt1, cancelbookingtxt;
	JDateChooser checkin1, checkout2;
	JComboBox bookingtypetxt1;
	JButton bookingbtn1;

	public Demo() {

		JFrame frame = new JFrame();
		frame.setSize(950, 550);
		frame.setLayout(null);
		;

		JLabel bookinglbl1 = new JLabel("CANCEL BOOKING");
		bookinglbl1.setBounds(80, 30, 250, 35);
		bookinglbl1.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(bookinglbl1);

		JLabel customerid1 = new JLabel("Booking ID:");
		customerid1.setVisible(false);
		customerid1.setBounds(30, 100, 200, 35);
		customerid1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(customerid1);

		customeridtxt1 = new JTextField();
		customeridtxt1.setVisible(false);
		customeridtxt1.setEnabled(false);
		customeridtxt1.setBounds(170, 100, 200, 30);
		customeridtxt1.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
		customeridtxt1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(customeridtxt1);

		JLabel bookingidlbl = new JLabel("Booking ID:");
		bookingidlbl.setBounds(30, 100, 200, 35);
		bookingidlbl.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(bookingidlbl);

		cancelbookingtxt = new JTextField();
		cancelbookingtxt.setBounds(170, 100, 200, 30);
		cancelbookingtxt.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
		cancelbookingtxt.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(cancelbookingtxt);

		JLabel label1 = new JLabel("Check-In: ");
		label1.setBounds(30, 150, 100, 35);
		label1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(label1);

		checkin1 = new JDateChooser();
		checkin1.setDateFormatString("yyyy-MM-dd");
		checkin1.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
		checkin1.setFont(new Font("Verdana", Font.PLAIN, 15));
		checkin1.setBounds(170, 150, 200, 30);
		frame.add(checkin1);

		JLabel checkout1 = new JLabel("Check-out:");
		checkout1.setBounds(30, 200, 200, 35);
		checkout1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(checkout1);

		checkout2 = new JDateChooser();
		checkout2.setDateFormatString("yyyy-MM-dd");
		checkout2.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
		checkout2.setFont(new Font("Verdana", Font.PLAIN, 15));
		checkout2.setBounds(170, 200, 200, 30);
		frame.add(checkout2);

		JLabel bookingtype1 = new JLabel("Room Type:");
		bookingtype1.setBounds(30, 250, 200, 35);
		bookingtype1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(bookingtype1);

		Object[] h2 = { "Single", "Twin", "Double" };

		bookingtypetxt1 = new JComboBox(h2);
		bookingtypetxt1.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
		bookingtypetxt1.setBounds(170, 250, 200, 30);
		bookingtypetxt1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(bookingtypetxt1);

		bookingbtn1 = new JButton("Cancel Booking");
		bookingbtn1.setBounds(30, 340, 200, 35);
		bookingbtn1.setBackground(new Color(21, 159, 133));
		bookingbtn1.setForeground(Color.white);
		bookingbtn1.setFont(new Font("Verdana", Font.PLAIN, 18));
		frame.add(bookingbtn1);
		bookingbtn1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == bookingbtn1) {
					BookingLibs booking = new BookingLibs();

					int cancelbooking = (Integer.parseInt(cancelbookingtxt.getText()));

					booking.setBooking_ID(cancelbooking);

					JDBCBooking jdbc2 = new JDBCBooking();
					boolean result1 = jdbc2.delete(cancelbooking);
					if (result1 == true) {
						updateTable();

						
						JOptionPane.showMessageDialog(null, "Your booking is cancelled");
					} else {
						JOptionPane.showMessageDialog(null, "Error");
								
					}

				}

			}

		});

		columnsName = new Object[7];
		columnsName[0] = "Customer ID";
		columnsName[1] = "Name";
		columnsName[2] = "Booking ID";
		columnsName[3] = "Check-In";
		columnsName[4] = "Check-Out";
		columnsName[5] = "Room Type";
		columnsName[6] = "Room Status";

		table1 = new JTable();
		model = (DefaultTableModel) table1.getModel();
		model.setColumnIdentifiers(columnsName);

		updateTable();
		JScrollPane scroll1 = new JScrollPane(table1);
		scroll1.setBounds(400,20,500,400);
		frame.add(scroll1, BorderLayout.CENTER);

		frame.setVisible(true);

	}

	public void updateTable() {
		a1 = new BookingJDBC().view_all1();
		model.setRowCount(0);
		for (BookingLibs2 bookingLibs : a1) {
			Object tmpRow[] = { bookingLibs.getCustomer_ID(),
					bookingLibs.getName(),
					bookingLibs.getBooking_ID(),
					bookingLibs.getCheckIn(),
					bookingLibs.getCheckOut(),
					bookingLibs.getBooking_Type(),
					bookingLibs.getBooking_Status()

			};

			model.addRow(tmpRow);
		}
		table1 = new JTable(model);

		
	}

	public static void main(String[] args) {
		new Demo();

	}

}
JDBC Library
package Libs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class BookingJDBC {
	
	public ArrayList view_all1() {
		Connection conn;
		PreparedStatement pstat;
		ResultSet rs;
		ArrayLista1=new ArrayList();
		String sql="SELECT Customer_ID, Name, Booking_ID, CheckIn, CheckOut,Booking_Type, "
				+ "Booking_Status  FROM booking INNER JOIN customers USING (Customer_ID)"
				+ "ORDER BY Booking_ID;";
		
		
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel_database","root","");
			
			pstat=conn.prepareStatement(sql);
		
			rs=pstat.executeQuery();
			
			while(rs.next()) {
				BookingLibs2 booking=new BookingLibs2(rs.getInt("Customer_ID"),
						rs.getString("Name"),
						rs.getInt("Booking_ID"),
						rs.getString("CheckIn"),
						rs.getString("CheckOut"),
						rs.getString("Booking_Type"),
						rs.getString("Booking_Status")
						);
				
				a1.add(booking);
				
			}
			
			
		}
		catch(Exception ex) {
			System.out.println("Error"+ex.getMessage());
		}
		return a1;
		
	}

}
Library
package Libs;

public class BookingLibs2 {

	int Customer_ID;
	String Name;
	int Booking_ID;
	String CheckIn;
	String CheckOut;
	String Booking_Type;
	String Booking_Status;
	
	
	public BookingLibs2() {
		
		this.Customer_ID = 0;
		this.Name = "";
		this.Booking_ID = 0;
		this.CheckIn = "";
		this.CheckOut = "";
		this.Booking_Type = "";
		this.Booking_Status = "";
	}


	public BookingLibs2(int customer_ID, String name, int booking_ID, String checkIn, String checkOut, 
			String booking_Type, String booking_Status) {
		
		this.Customer_ID = customer_ID;
		this.Name = name;
		this.Booking_ID = booking_ID;
		this.CheckIn = checkIn;
		this.CheckOut = checkOut;
		this.Booking_Type = booking_Type;
		this.Booking_Status = booking_Status;
	}


	public int getCustomer_ID() {
		return Customer_ID;
	}


	public void setCustomer_ID(int customer_ID) {
		Customer_ID = customer_ID;
	}


	public String getName() {
		return Name;
	}


	public void setName(String name) {
		Name = name;
	}


	public int getBooking_ID() {
		return Booking_ID;
	}


	public void setBooking_ID(int booking_ID) {
		Booking_ID = booking_ID;
	}


	public String getCheckIn() {
		return CheckIn;
	}


	public void setCheckIn(String checkIn) {
		CheckIn = checkIn;
	}


	public String getCheckOut() {
		return CheckOut;
	}


	public void setCheckOut(String checkOut) {
		CheckOut = checkOut;
	}


	public String getBooking_Type() {
		return Booking_Type;
	}


	public void setBooking_Type(String booking_Type) {
		Booking_Type = booking_Type;
	}


	public String getBooking_Status() {
		return Booking_Status;
	}


	public void setBooking_Status(String booking_Status) {
		Booking_Status = booking_Status;
	}


	@Override
	public String toString() {
		return "BookingLibs2 [Customer_ID=" + Customer_ID + ", Name=" + Name + ", Booking_ID=" + Booking_ID
				+ ", CheckIn=" + CheckIn + ", CheckOut=" + CheckOut + ", Booking_Type=" + Booking_Type
				+ ", Booking_Status=" + Booking_Status + "]";
	}


	

}

private void update() {
		 JDBCCustomer jdbc = new JDBCCustomer();
			ArrayList select = jdbc.select_all();
			model.setRowCount(0);
			if (select.size() > 0) {
	            for (int i = 0; i < select.size(); i++) {
	                CustomerLibs tmp_person = (CustomerLibs) select.get(i);
	                
	                Vector tmpPerson = new Vector();
	                
	                tmpPerson.add(tmp_person.getCustomer_ID());
	                tmpPerson.add(tmp_person.getTitle());
	                tmpPerson.add(tmp_person.getName());
	                tmpPerson.add(tmp_person.getGender());
	                tmpPerson.add(tmp_person.getDOB());
	                tmpPerson.add(tmp_person.getMobile());
	                tmpPerson.add(tmp_person.getEmail());
	                tmpPerson.add(tmp_person.getAddress());
	                tmpPerson.add(tmp_person.getUsername());
	                tmpPerson.add(tmp_person.getPassword());
	                tmpPerson.add(tmp_person.getCredit_Number());
	               
	                
	                model.addRow(tmpPerson);
	            }
	        }
	    }
Output
Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.