Report Java
Report Java
The Online Train Reservation System integration is a robust and user-friendly application
designed to simplify and automate the process of booking train tickets online. Developed using
Java, the project employs Java Database Connectivity (JDBC) for efficient interaction with the
MySQL database. The system offers features such as user registration and login, train search and
booking, fare calculation.
Additionally, the project embraces principles of data security by encrypting sensitive user
information and employing secure authentication mechanisms. The admin panel offers
functionalities for managing train schedules, user accounts, and monitoring reservations. The
integration of JDBC ensures that any changes made through the admin panel are efficiently
reflected in the database.
Java Swing
Java Swing is a set of graphical user interface (GUI) components for Java applications. It
provides a rich set of controls and components for building desktop applications with a
consistent look and feel.
In a Train Reservation System project developed in Java, the use of Java Swing provides a
graphical user interface (GUI) for a more user-friendly and interactive experience.It uses the
components like JTextFields, Jlabel, JButton, etc.
JFrames and JPanels: Java Swing provides classes like JFrame and JPanel for creating
the main application window and organizing components within it. The overall structure
of the application is built using these containers.
JLabels and JTextFields: Swing components like JLabel and JTextField are used for
displaying information and capturing user input. For example, labels can be used to show
information about the train, and text fields can be used for users to input details like
source, destination, and the number of passengers.
JButtons: JButton components are used to create clickable buttons for actions like
searching for trains, booking tickets, or navigating between different sections of the
application.
Write and Execute the Query with the help of Statement and PreparedStatement
Connection Closed
Event Handling
Event handling in Java refers to the mechanism by which events, such as user actions or system-
generated occurrences, are managed and responded to in a Java program. Java provides a robust
event-handling framework that allows developers to create responsive and interactive
applications.
In Java, event handling involves three main components: sources, listeners, and events. A source
is an object that generates an event, such as a button or a mouse click. A listener is an interface
or class that receives and processes events. Events are objects representing occurrences, and they
encapsulate information about the event, such as its type or source.
ActionListener
The ActionListener interface in Java is a part of the java.awt.event package and is a key
component of the event-handling mechanism. It is used to handle action events, typically
generated by user interactions with graphical user interface (GUI) components like
buttons and menu items.
Program code :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.MatteBorder;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.InsetsUIResource;
JLabel l1;
JLabel l2;
JTextField t1;
JPasswordField t2;
JButton submit;
JCheckBox c1;
JLabel user;
JLabel pass;
JLabel phone;
JLabel email;
JLabel renter;
JTextField nametxt;
JTextField mailtxt;
JTextField phntxt;
JButton register;
JPasswordField pfield;
JPasswordField rfield;
JCheckBox c2;
JCheckBox c3;
ColoredJTabbedPane() {
var1.setLayout(new BorderLayout());
var1.add(this.header);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
var8.printStackTrace();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
var7.printStackTrace();
var4.setLayout((LayoutManager)null);
this.getContentPane().add(var2);
var2.setBorder(var5);
var1.add(var2);
this.l1.setFont(var6);
this.l2.setFont(var6);
this.submit.setFont(var6);
this.submit.addActionListener(this);
var3.add(this.l1);
var3.add(this.l2);
var3.add(this.t1);
var3.add(this.t2);
var3.add(this.c1);
this.c1.addActionListener(this);
var3.add(this.submit);
this.user.setFont(var6);
this.pass.setFont(var6);
this.renter.setFont(var6);
this.email.setFont(var6);
this.register.setFont(var6);
var4.add(this.user);
var4.add(this.email);
var4.add(this.phone);
var4.add(this.pass);
var4.add(this.renter);
var4.add(this.nametxt);
var4.add(this.mailtxt);
var4.add(this.phntxt);
var4.add(this.pfield);
var4.add(this.rfield);
var4.add(this.register);
this.register.addActionListener(this);
this.c2.addActionListener(this);
this.c3.addActionListener(this);
var4.add(this.c2);
var4.add(this.c3);
String var2;
String var3;
if (var1.getSource() == this.register) {
System.out.println("Register Called");
var2 = this.nametxt.getText();
var3 = this.mailtxt.getText();
String var4 = this.phntxt.getText();
if (var1.getSource() == this.submit) {
if (this.t1.getText().isEmpty()) {
var2 = null;
} else {
var2 = this.t1.getText();
if (this.t2.getText().isEmpty()) {
var3 = null;
} else {
var3 = this.t2.getText();
}
public static void main(String[] var0) {
var1.setSize(600, 600);
var1.setVisible(true);
var1.setDefaultCloseOperation(3);
var1.setLocation(600, 100);
2) import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;
SwingUtilities.invokeLater(() -> {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tabbedPane);
frame.setSize(400, 300);
frame.setVisible(true);
});
@Override
}}
3) import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.table.*;
import javafx.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.awt.*;
public DatabaseTableExample() {
setSize(700,700);
// Create the database connection
table.setBackground(Color.green);
table.setBounds(20,20,700,500);
header.setDefaultRenderer(new HeaderRenderer(table));
table.setSize(700, 600);
table.setRowHeight(30); // Set cell height to 50
this.add(scrollPane);
} catch (Exception e) {
e.printStackTrace();
columnNames[column - 1] = metaData.getColumnName(column);
}
// Create a DefaultTableModel and add data to it
while (resultSet.next()) {
rowData[i - 1] = resultSet.getObject(i);
tableModel.addRow(rowData);
tableModel.addColumn("Book ");
return tableModel;
setHorizontalAlignment(SwingConstants.CENTER);
setSize(100, 50);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
}
4) import javax.swing.*;
import javax.swing.event.DocumentListener;
import javax.xml.crypto.dsig.spec.XPathType.Filter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
public Filters() {
while (startDate.before(endDate)) {
dateComboBoxModel.addElement(dateFormatter.format(startDate.getTime()));
dateComboBox.setModel(dateComboBoxModel);
this.add(dateComboBox);
trainClassesComboBox.setModel(new
DefaultComboBoxModel<>(getIndianTrainClasses()));
this.add(trainClassesComboBox);
"First AC (1A)",
"Second AC (2A)",
"Third AC (3A)",
};
return trainClasses;
}
5) import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
//panel 1
JTextField t1;
JPasswordField t2;
JButton submit;
JCheckBox c1;
//panel 2
JButton register;
index() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
tabbedPane.setUI(new BasicTabbedPaneUI() {
if (isSelected) {
g.fillRect(x, y, w, h);
protected void paintTab(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int
h, boolean isSelected) {
if (isSelected) {
g.fillRect(x, y, w, h);
});
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
panel1.setLayout(null);
panel2.setLayout(null);
Color.BLACK));
getContentPane().add(tabbedPane);
Color creamColor = new Color(255, 253, 208);
tabbedPane.setBorder(upperBorder);
c.add(tabbedPane);
tabbedPane.setBackground(Color.RED);
//panel1
l1 = new JLabel("UserName");
l1.setFont(f1);
l2 = new JLabel("Password");
l2.setFont(f1);
t1 = new JTextField(20);
t2 = new JPasswordField(8);
submit.setFont(f1);
c1 = new JCheckBox("Show");
c1.setBackground(creamColor);
panel1.add(l1);
panel1.add(l2);
panel1.add(t1);
panel1.add(t2);
panel1.add(c1);
c1.addActionListener(new ActionListener() {
if (c1.isSelected()) {
t2.setEchoChar((char) 0);
} else {
t2.setEchoChar('*');
});
panel1.add(submit);
//panel2
user.setFont(f1);
pass.setFont(f1);
email.setFont(f1);
phone.setFont(f1);
c2 = new JCheckBox("Show");
c3 = new JCheckBox("Show");
c2.setBackground(creamColor);
c3.setBackground(creamColor);
panel2.add(user);
panel2.add(email);
panel2.add(phone);
panel2.add(pass);
panel2.add(renter);
panel2.add(nametxt);
panel2.add(mailtxt);
panel2.add(phntxt);
panel2.add(pfield);
panel2.add(rfield);
panel2.add(register);
c2.addActionListener(new ActionListener() {
@Override
if (c2.isSelected()) {
pfield.setEchoChar((char) 0);
} else {
pfield.setEchoChar('*');
});
c3.addActionListener(new ActionListener() {
@Override
if (c3.isSelected()) {
rfield.setEchoChar((char) 0);
} else {
rfield.setEchoChar('*');
});
panel2.add(c2);
panel2.add(c3);
t1.setSize(600, 600);
t1.setVisible(true);
t1.setDefaultCloseOperation(EXIT_ON_CLOSE);
t1.setLocation(600, 100);
}
6)import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javafx.event.ActionEvent;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.awt.*;
//p1
JLabel header;
//p3
JLabel source,destination;
JTextField sourcein,destin;
//connection
String jdbcUrl = "jdbc:mysql://localhost:3306/train_reservation_system";
Reservation(){
setSize(screenSize.width, screenSize.height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p1.setSize(100,100);
p1.setBackground(new Color(217,167,176));
p2.setSize(100,100);
p2.setBackground(new Color(166,111,111));
p3.setBackground(new Color(167,203,217));
JPanel cenp = new JPanel();
cenp.setSize(700,800);
p4.setSize(100,100);
p4.setBackground(new Color(242,224,213));
p5.setSize(100,100);
p5.setBackground(new Color(104,160,166));
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(p1,BorderLayout.NORTH);
c.add(p2,BorderLayout.EAST);
c.add(p3,BorderLayout.CENTER);
c.add(p4,BorderLayout.WEST);
c.add(p5,BorderLayout.SOUTH);
//p1
header.setFont(texts);
header.setBounds(300,20,800,70);
p1.add(header);
//p3
source.setFont(f1);
destination.setFont(f1);
sourcein.setBackground(new Color(167,203,217));
destin.setBackground(new Color(167,203,217));
source.setBounds(20,10,100,25);
sourcein.setBounds(130,10,150,25);
destination.setBounds(400,10,150,25);
destin.setBounds(550,10,150,25);
t1.setBounds(30,100,800,600);
p3.add(source);
p3.add(sourcein);
p3.add(destination);
p3.add(destin);
p3.add(t1);
//p2
date.setBackground(new Color(242,224,213));
p4.add(date);
setVisible(true);
}
7)import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
tab1.setToolTipText("Tab 1 Tooltip");
tab2.setToolTipText("Tab 2 Tooltip");
@Override
return component;
};
tabbedPane.setTabComponentAt(0, tab1);
tabbedPane.setTabComponentAt(1, tab2);
frame.add(tabbedPane);
frame.setVisible(true);
}
Output
1- Login Page
2- Register Page
:
3- Route Selection
On this page the user need to provide Source and Destination and according to it whether the
train is available or not will be shown to user.
4- Train Selection
This page will display the available train for the route mentioned by user and user need to select
the train according to his/her need.
5- Passenger Details
This page will request the user to enter the passenger. At first the user need to enter the no. of
passenger than new panel will display and on that the user need to give the name and age of the
passengers.
6- Payment Overview
7- Downloaded Ticket
Conclusion:
In this way, we completed our micro project and developed the Train Reservation System
which is user-friendly Java-based application that uses JDBC and Swing for efficient database
connectivity and a graphical interface.