-
Notifications
You must be signed in to change notification settings - Fork 3
/
bus.java
146 lines (131 loc) · 4.78 KB
/
bus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package bus_booking;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class bus{
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
Connection c = null;
Statement stmt = null;
int no_of_seats;
String bus_owner;
String bus_driver;
String bus_conductor;
String bus_id;
int route_no;
bus()
{
}
public
void addBus(int no_of_seats,String bus_owner,String bus_driver,String bus_conductor,String bus_id, int route_no)
{
this.no_of_seats=no_of_seats;
this.bus_owner=bus_owner;
this.bus_driver=bus_driver;
this.bus_conductor=bus_conductor;
this.bus_id=bus_id;
this.route_no=route_no;
try
{
// This will load the MySQL driver, each DB has its own driver
Class.forName("org.postgresql.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bus_ticket_booking",
"postgres", "postgres");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into bus values (?, ?,?,?,?,?)");
// Parameters start with 1
preparedStatement.setString(1, bus_conductor);
preparedStatement.setInt(2, route_no);
preparedStatement.setInt(3, no_of_seats);
preparedStatement.setString(4, bus_driver);
preparedStatement.setString(5, bus_owner);
preparedStatement.setString(6, bus_id);
preparedStatement.executeUpdate();
preparedStatement = connect
.prepareStatement("SELECT * from bus");
resultSet = preparedStatement.executeQuery();
System.out.print("New Bus added");
}
catch(Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
void searchBus(String bus_id)
{
try
{
// This will load the MySQL driver, each DB has its own driver
Class.forName("org.postgresql.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bus_ticket_booking",
"postgres", "postgres");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement( ("SELECT * from bus where bus_id=?"));
preparedStatement.setString(1, bus_id);
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
void editBus(String bus_id)
{
System.out.println("What do you want to edit?");
}
void deleteBus(String bus_id)
{
try {
Class.forName("org.postgresql.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bus_ticket_booking",
"postgres", "postgres");
statement = connect.createStatement();
preparedStatement = connect
.prepareStatement( ("DELETE from bus where bus_id = ? "));
preparedStatement.setString(1, bus_id);
System.out.println("Deleted successfully");
preparedStatement.executeQuery();
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException{
while (resultSet.next()) {
String bus_conductor = resultSet.getString("bus_conductor");
int route_no = resultSet.getInt("route_no");
int no_of_seats = resultSet.getInt("no_of_seats");
String bus_driver = resultSet.getString("bus_driver");
String bus_owner = resultSet.getString("bus_owner");
System.out.println( "Bus Conductor = " + bus_conductor );
System.out.println( "Route number = " + route_no );
System.out.println( "Number of seats = " + no_of_seats );
System.out.println( "Bus Driver = " + bus_driver );
System.out.println( "Bus Owner = " + bus_owner );
System.out.println();
}
}
};