-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train.java
63 lines (61 loc) · 2.46 KB
/
Train.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
import java.net.*;
import java.io.*;
import java.sql.*;
public class Train {
public static void main(String args[]) {
String Query;
ResultSet rs = null;
String msg;
int pnr = 0, trainnum = 0;
String from = new String();
String to = new String();
String date = new String();
String name = new String();
try {
try (ServerSocket ser = new ServerSocket(8000)) {
System.out.println("Server Started...");
Socket soc = null;
soc = ser.accept();
System.out.println("Received Connection: " + soc.getInetAddress().getHostAddress());
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:mydb", "scott", "tiger");
Statement stmt = conn.createStatement();
msg = in.readLine();
pnr = Integer.parseInt(msg);
Query = "Select * from train where pnrno = " + pnr;
rs = stmt.executeQuery(Query);
if (rs.next()) {
pnr = Integer.parseInt(rs.getString(1));
from = rs.getString(2);
to = rs.getString(3);
date = rs.getString(4);
name = rs.getString(5);
trainnum = Integer.parseInt(rs.getString(6));
} else {
System.out.println("invalid PNRNO");
}
} catch (SQLException e) {
System.out.println("Error Caught: " + e);
}
out.writeBytes(String.valueOf(pnr));
out.write(10);
out.writeBytes(from);
out.write(10);
out.writeBytes(to);
out.write(10);
out.writeBytes(date);
out.write(10);
out.writeBytes(name);
out.write(10);
out.writeBytes(String.valueOf(trainnum));
out.write(10);
soc.close();
}
} catch (Exception e) {
System.out.println("Error Caught: " + e);
}
}
}