Java

Advance Bus Ticket Booking System in Java Mysql

This tutorial will teach you Advanced Bus Booking System step by step.This system will use to manage the Bus Booking System.

The System shall be able to select the seat no which customer wants.

Step 1: implement  MouseListener 

lbl1.addMouseListener(this);
lbl2.addMouseListener(this);
lbl3.addMouseListener(this);
lbl4.addMouseListener(this);
lbl5.addMouseListener(this);
lbl6.addMouseListener(this);
lbl7.addMouseListener(this);
lbl8.addMouseListener(this);
lbl9.addMouseListener(this);
lbl10.addMouseListener(this);
lbl11.addMouseListener(this);
lbl12.addMouseListener(this);
Connect();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
txtdate.getDayChooser().setMinSelectableDate(date);

after implemented the MouseListener it ask to implement all the abstruct method of MouseListener.you must implement it.

after created all abstruct methods paste the code inside the mouseClicked method.

 public void mouseClicked(MouseEvent e) {
        if(e.getSource() == lbl1 )
        {
             seatno = 1;
        }
        else   if(e.getSource() == lbl2 )
        {
             seatno = 2;
        }
          else   if(e.getSource() == lbl3 )
        {
             seatno = 3;
        }
          else   if(e.getSource() == lbl4 )
        {
             seatno = 4;
        }
          else   if(e.getSource() == lbl5 )
        {
             seatno = 5;
        }
          else   if(e.getSource() == lbl6 )
        {
             seatno = 6;
        }
          else   if(e.getSource() == lbl7 )
        {
             seatno = 7;
        }
        
           else   if(e.getSource() == lbl8 )
        {
             seatno = 8;
        }
        
           else   if(e.getSource() == lbl9 )
        {
             seatno = 9;
        }
        
           else   if(e.getSource() == lbl10 )
        {
             seatno = 10;
        }
           else   if(e.getSource() == lbl11 )
        {
             seatno = 11;
        }
        
        else   if(e.getSource() == lbl12 )
        {
             seatno = 12;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent e) {
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
    //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

Estabilsh the database connection 

      Connection con;
        PreparedStatement pst;  
        int seatno=0;
        ResultSet rs;
        
       public void Connect()
        {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/buss","root","");
             
        } 
        catch (ClassNotFoundException ex) 
        {
            
        } 
        catch (SQLException ex) 
        {
           
        }

Paste code the inside the book button

        String customer = txtcust.getText();
        int seatno1 = seatno;
        String price = txtprice.getText();
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
        String date = date_format.format(txtdate.getDate());
               
        try {
            pst = con.prepareStatement("select * from book where date = ? and seatno = ?");
            pst.setString(1, date);
            pst.setInt(2, seatno1);
            rs = pst.executeQuery();
                          
            if(rs.next() == true)
            {       
            JOptionPane.showMessageDialog(this, "Sorry this has been Booked");      
            }
          
          else
          {
           try 
           {
            pst = con.prepareStatement("insert into book(cname,seatno,price,date)values(?,?,?,?)");
            pst.setString(1, customer);
            pst.setInt(2, seatno1);
            pst.setString(3, price);
            pst.setString(4, date);
            int k = pst.executeUpdate();
            
            if(k==1)
            {
                JOptionPane.showMessageDialog(this, "Bus Booked");
                bill();
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Record Failed");
            }

        } catch (SQLException ex) {
           
        } 
        }

        } catch (SQLException ex) {
            Logger.getLogger(busadvanc.class.getName()).log(Level.SEVERE, null, ex);
        }

Search Seats

The System shall be to search the seats.

    Connection con;
    PreparedStatement pst; 
    ResultSet rs;
    int seatno=0;   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = date_format.format(txtdate.getDate());
        int c;
        
        try 
        {
            pst = con.prepareStatement("select * from book where date = ?");
            pst.setString(1, date1);
            rs = pst.executeQuery();                
            if(rs.next() == false)
              {       
              JOptionPane.showMessageDialog(this, "Date Not Found");
              }
            else
                {
                    ResultSetMetaData rsd = rs.getMetaData();
                    c = rsd.getColumnCount();
                    DefaultTableModel d = (DefaultTableModel)jTable1.getModel();
                    d.setRowCount(0);
            while(rs.next())
            {
                Vector v2 = new Vector();
                
                for(int i= 1; i<=c; i++)
                {
                    v2.add(rs.getString("id"));
                    v2.add(rs.getString("cname"));
                    v2.add(rs.getString("seatno"));
                    v2.add(rs.getString("price"));
             
                }
                d.addRow(v2);
            }
             }  
            
        } catch (SQLException ex) {
            Logger.getLogger(check.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

 

i have attached the video link below. which will do this tutorials step by step.

 

 

 

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

2 days ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

4 days ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

4 days ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

1 week ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

3 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

4 weeks ago