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

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

7 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

3 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago