-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathBookingServiceImpl.java
81 lines (74 loc) · 2.59 KB
/
BookingServiceImpl.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
package com.shashi.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import com.shashi.beans.HistoryBean;
import com.shashi.beans.TrainException;
import com.shashi.constant.ResponseCode;
import com.shashi.service.BookingService;
import com.shashi.utility.DBUtil;
public class BookingServiceImpl implements BookingService {
@Override
public List<HistoryBean> getAllBookingsByCustomerId(String customerEmailId) throws TrainException {
List<HistoryBean> transactions = null;
String query = "SELECT * FROM HISTORY WHERE MAILID=?";
try {
Connection con = DBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, customerEmailId);
ResultSet rs = ps.executeQuery();
transactions = new ArrayList<HistoryBean>();
while (rs.next()) {
HistoryBean transaction = new HistoryBean();
transaction.setTransId(rs.getString("transid"));
transaction.setFrom_stn(rs.getString("from_stn"));
transaction.setTo_stn(rs.getString("to_stn"));
transaction.setDate(rs.getString("date"));
transaction.setMailId(rs.getString("mailid"));
transaction.setSeats(rs.getInt("seats"));
transaction.setAmount(rs.getDouble("amount"));
transaction.setTr_no(rs.getString("tr_no"));
transactions.add(transaction);
}
ps.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new TrainException(e.getMessage());
}
return transactions;
}
@Override
public HistoryBean createHistory(HistoryBean details) throws TrainException {
HistoryBean history = null;
String query = "INSERT INTO HISTORY VALUES(?,?,?,?,?,?,?,?)";
try {
Connection con = DBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(query);
String transactionId = UUID.randomUUID().toString();
ps.setString(1, transactionId);
ps.setString(2, details.getMailId());
ps.setString(3, details.getTr_no());
ps.setString(4, details.getDate());
ps.setString(5, details.getFrom_stn());
ps.setString(6, details.getTo_stn());
ps.setLong(7, details.getSeats());
ps.setDouble(8, details.getAmount());
int response = ps.executeUpdate();
if (response > 0) {
history = (HistoryBean) details;
history.setTransId(transactionId);
} else {
throw new TrainException(ResponseCode.INTERNAL_SERVER_ERROR);
}
ps.close();
} catch (SQLException e) {
System.out.println("error is " + e.getMessage());
throw new TrainException(e.getMessage());
}
return history;
}
}