-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectFile2.java
398 lines (349 loc) · 17.8 KB
/
ProjectFile2.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import javax.swing.table.DefaultTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author azahar shareef
*/
public class Stations extends javax.swing.JFrame {
Connection con;
public Stations() {
initComponents();
createConection();
}
void createConection(){
try{
Class.forName("com.mysql.jdbc.Driver");//we are adding drivers now..(before loading driver connector to library of my project)
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/RailwayReservation","root","Azaharjaveedshareef@12");
System.out.println("connection sucessfull..!!");
}catch(Exception e){
System.out.println("an exception occured while loading drivers or connecting..!!");
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
Stations = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
Source = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
Name = new javax.swing.JTextArea();
jScrollPane5 = new javax.swing.JScrollPane();
Age = new javax.swing.JTextArea();
jScrollPane6 = new javax.swing.JScrollPane();
Sex = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane7 = new javax.swing.JScrollPane();
Destination = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
BookTicket = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Stations Present::");
setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
setPreferredSize(new java.awt.Dimension(1600, 900));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jButton1.setForeground(new java.awt.Color(0, 0, 255));
jButton1.setText("Click Here To View Train Timings At The Stations::");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
Stations.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"StationID", "StationName", "ArrivalTime", "DepatureTime", "TrainId"
}
));
jScrollPane1.setViewportView(Stations);
Source.setColumns(20);
Source.setRows(5);
jScrollPane2.setViewportView(Source);
Name.setColumns(20);
Name.setRows(5);
jScrollPane4.setViewportView(Name);
Age.setColumns(20);
Age.setRows(5);
jScrollPane5.setViewportView(Age);
Sex.setColumns(20);
Sex.setRows(5);
jScrollPane6.setViewportView(Sex);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(20, 255, 40));
jLabel1.setText("Source");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel2.setForeground(new java.awt.Color(64, 255, 44));
jLabel2.setText("Destination");
Destination.setColumns(20);
Destination.setRows(5);
jScrollPane7.setViewportView(Destination);
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel3.setForeground(new java.awt.Color(64, 255, 50));
jLabel3.setText("sex");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel4.setForeground(new java.awt.Color(64, 255, 50));
jLabel4.setText("Age");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jLabel5.setForeground(new java.awt.Color(64, 255, 50));
jLabel5.setText("Name");
BookTicket.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
BookTicket.setForeground(new java.awt.Color(255, 0, 0));
BookTicket.setText("BookTicket");
BookTicket.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BookTicketActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
.addGap(18, 18, 18))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(53, 53, 53)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane5)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BookTicket, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(80, 80, 80)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(61, 61, 61)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addComponent(BookTicket, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(24, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel tableModel=(DefaultTableModel) Stations.getModel();
try{
/* Statement stmt1=con.createStatement();
ResultSet rs1=stmt1.executeQuery("select *from temp");
String titi=rs1.getString("change");
System.out.println("train temp is::"+titi);
*/
Statement stmt1 = con.createStatement();
ResultSet rs1;
String change = null;
rs1 = stmt1.executeQuery("select *from temp");
while(rs1.next())
{
String s=rs1.getString("change");
change=s;
System.out.println(s+" "+change);
}
// System.out.println("change is::"+change);
//int i=Integer.parseInt(change);
//System.out.println("i si integer"+(i+1));
// System.out.println("temp change is::"+s);
PreparedStatement stmt19=con.prepareStatement("truncate temp");
stmt19.execute();
Statement stmt = con.createStatement();
ResultSet rs;
String lol="select *from stations where trainid="+change;
rs = stmt.executeQuery(lol);
while(rs.next())
{
int ti=rs.getInt("Trainid");
String sn=rs.getString("StationName");
String si=rs.getString("Stationid");
String at=rs.getString("ArrivalTime");
String dt=rs.getString("DepartureTime");
//System.out.println(tn+" "+ti);
tableModel.addRow(new Object[]{si,sn,at,dt,ti} );
}
stmt.close();
}
catch(Exception e)
{
System.out.println("javeed.........");
}
}
/* public String GenerateTicketId()
{
try{
int count=21984;
Statement stmt10 = con.createStatement();
ResultSet rs10;
rs10 = stmt10.executeQuery("select *from users");
while(rs10.next())
{
count+=7;
}
String ans=String.valueOf(count);
System.out.println("ans is:::"+ans);
stmt10.close();
return ans;
}catch(Exception ex){
// Logger.getLogger(DatabaseProject1.class.getName()).log(Level.SEVERE,null,ex);
System.out.println("Not able to connect to server database..!!!");
}
return "ohoo..!!";
}*/
private void BookTicketActionPerformed(java.awt.event.ActionEvent evt) {
try{
String name=Name.getText();
String source=Source.getText();
String destination=Destination.getText();
String age=Age.getText();
String sex=Sex.getText();
Random rand=new Random();
int count=6464+rand.nextInt(1000);
//System.out.println("generated ticket id is::"+count);
String tid=String.valueOf(count);
/*try{
int y=tid+2;
System.out.println("sucessfull..!!");
}catch(Exception e){
System.out.println("Not changed..!!");
}*/
System.out.print("string is::"+tid);
//String sql=
// PreparedStatement stmt2=con.prepareStatement(sql);
// Stmt2.setString()
// String sql=null;
PreparedStatement stmt11=con.prepareStatement("insert into ticket values(?,?,?,?,?,?)");
stmt11.setString(1,tid);
stmt11.setString(2,name);
stmt11.setString(3,age);
stmt11.setString(4,sex);
stmt11.setString(5,source);
stmt11.setString(6,destination);
stmt11.execute();
stmt11.close();
PreparedStatement stmt14=con.prepareStatement("insert into booking values(?,?,?,?,?,?)");
stmt14.setString(1,tid);
stmt14.setString(2,name);
stmt14.setString(3,age);
stmt14.setString(4,sex);
stmt14.setString(5,source);
stmt14.setString(6,destination);
stmt14.execute();
stmt14.close();
System.out.println("Sucessfully booking completed..!!");
Ticket jaan=new Ticket();
jaan.fun();
}catch(Exception e)
{
}
}
/**
* @param args the command line arguments
*/
public void fun(){
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Stations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Stations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Stations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Stations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run() {
new Stations().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea Age;
private javax.swing.JButton BookTicket;
private javax.swing.JTextArea Destination;
private javax.swing.JTextArea Name;
private javax.swing.JTextArea Sex;
private javax.swing.JTextArea Source;
private javax.swing.JTable Stations;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JScrollPane jScrollPane6;
private javax.swing.JScrollPane jScrollPane7;
// End of variables declaration
}