Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. package HotelInterface;
  2.  
  3. import java.math.BigInteger;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.sql.*;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import static HotelInterface.CustomerDetails.customerID;
  12.  
  13.  
  14. public class Jdbc {
  15.  
  16. static String customerEmail;
  17. Connection myConn = null;
  18. Statement myStmt = null;
  19. ResultSet myRs = null;
  20.  
  21. public Jdbc() throws SQLException{
  22. try {
  23. // go();
  24. myConn = DriverManager.getConnection("jdbc:mysql://localhost:2222/w1495485_0?zeroDateTimeBehavior=convertToNull", "w1495485", "GBlmPeUulqaX");
  25. myStmt = myConn.createStatement();
  26. } catch (Exception exc) {
  27. exc.printStackTrace();
  28. }
  29. }
  30.  
  31. public String getCustomerID(){
  32. return customerEmail;
  33.  
  34. }
  35.  
  36. public void RegisterStaff(String FirstName, String LastName, String UserName, String Password, String StaffType) {
  37. try {
  38. String sql = "insert into Staff " + " (FirstName,LastName,UserName,Password,StaffType)" +
  39. " values ('" + FirstName + "', '" + LastName + "','" + UserName + "','" + getMD5(Password) + "','" + StaffType + "')";
  40. myStmt.executeUpdate(sql);
  41. System.out.println("Inserted");
  42.  
  43. } catch (Exception exc) {
  44. exc.printStackTrace();
  45. }
  46. }
  47.  
  48. public Boolean StaffLogin(String user, String pass) {
  49. try {
  50. String sql = "SELECT UserName,Password FROM Staff where UserName=? and Password=?";
  51. PreparedStatement ps = null;
  52. ps = myConn.prepareStatement(sql);
  53. ps.setString(1, user);
  54. ps.setString(2, getMD5(pass));
  55. ResultSet rs = ps.executeQuery();
  56. if (rs.next()) {
  57. return true;
  58. } else {
  59. return false;
  60. }
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. return null;
  65. }
  66.  
  67. // public int getCustomerID(){}
  68.  
  69. public Boolean CustomerLogin(String user, String pass) {
  70. try {
  71. String sql = "SELECT Email,Password FROM Customer where Email=? and Password=?";
  72. PreparedStatement ps = null;
  73. ps = myConn.prepareStatement(sql);
  74. ps.setString(1, user);
  75. ps.setString(2, getMD5(pass));
  76. ResultSet rs = ps.executeQuery();
  77.  
  78.  
  79.  
  80.  
  81. if (rs.next()) {
  82.  
  83. String cust = new String(rs.getObject(1).toString());
  84. customerEmail = cust;
  85. System.out.println("Customer ID in JDBC = " +customerEmail);
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. } catch (SQLException e) {
  91. e.printStackTrace();
  92. }
  93. return null;
  94. }
  95.  
  96.  
  97.  
  98. public void ShowTable() {
  99. try {
  100. myRs = myStmt.executeQuery("select * from Staff");
  101. int n = 0;
  102. while (myRs.next()) // while there's still some more results of the query...
  103. {
  104. int numColumns = myRs.getMetaData().getColumnCount(); // get how many cols in this entry
  105. n++;
  106. System.out.print("" + n);
  107. for (int i = 1; i <= numColumns; i++) // loop through the cols to print them
  108. { // Column numbers start at 1.
  109. System.out.print(" " + myRs.getObject(i));
  110. }
  111. System.out.println(""); // print a new line at the end of the entry.
  112. }
  113. } catch (Exception exc) {
  114. exc.printStackTrace();
  115. }
  116. }
  117.  
  118. public void Delete(String ID) {
  119. try {
  120. String sql = "delete from Staff where ID = '" + ID + "'";
  121. int rowsAffeted = myStmt.executeUpdate(sql);
  122. } catch (Exception exc) {
  123. exc.printStackTrace();
  124. }
  125. }
  126.  
  127. public static String getMD5(String input) {
  128. try {
  129. MessageDigest md = MessageDigest.getInstance("MD5");
  130. byte[] messageDigest = md.digest(input.getBytes());
  131. BigInteger number = new BigInteger(1, messageDigest);
  132. String hashtext = number.toString(16);
  133. // Now we need to zero pad it if you actually want the full 32 chars.
  134. while (hashtext.length() < 32) {
  135. hashtext = "0" + hashtext;
  136. }
  137. return hashtext;
  138. } catch (NoSuchAlgorithmException e) {
  139. throw new RuntimeException(e);
  140. }
  141. }
  142.  
  143. public void Update(String field, String value, int id) {
  144. try {
  145. String sql = "update Staff set " + field + "='" + value + " ' where id=" + id + "";
  146. int rowsAffected = myStmt.executeUpdate(sql);
  147. System.out.println("Rows affected: " + rowsAffected);
  148. System.out.println("Updated");
  149.  
  150. } catch (SQLException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154.  
  155. public void addDate(String date, String date1) throws ParseException, SQLException {
  156.  
  157. java.util.Date utilDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
  158. java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
  159.  
  160. java.util.Date utilDate1 = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
  161. java.sql.Date sqlDate1 = new java.sql.Date(utilDate.getTime());
  162.  
  163. PreparedStatement p = myConn.prepareStatement("INSERT INTO `w1495485_0`.`Booking` (`BookingID` ,`StartDate` ,`EndDate` ,`CustomerID` ,`RoomID`)VALUES (NULL , ?, ?, '1', '1');");
  164.  
  165. p.setDate(1, sqlDate);
  166. p.setDate(2, sqlDate1);
  167. p.execute();
  168.  
  169. // try {
  170. // LocalDate date = checkin.getValue();
  171. // System.out.println(date);
  172. // db = new Jdbc();
  173. // db.addDate(date.toString());
  174. //
  175. // } catch (SQLException e) {
  176. // e.printStackTrace();
  177. // } catch (ParseException e) {
  178. // e.printStackTrace();
  179. // }
  180. }
  181.  
  182. public List RoomCheck(String column, String roomSize) {
  183. List rowValues = null;
  184. try {
  185. rowValues = new ArrayList();
  186. myRs = null;
  187. myRs = myStmt.executeQuery("SELECT " + column + " FROM Booking,Room where Room.RoomSize = \"" + roomSize + "\"");
  188. //select startdate from booking where roomid = (select roomid from room where roomsize = large)
  189. while (myRs.next()) {
  190. rowValues.add(myRs.getString(1));
  191. }
  192. // for (int i = 0; i < rowValues.size(); i++) {
  193. // System.out.println(rowValues.get(i));
  194. // }
  195. } catch (SQLException e) {
  196. e.printStackTrace();
  197. }
  198. return rowValues;
  199. }
  200.  
  201. public void addUserToDB(String fname, String lname, String password, String Address, String email, String Country, String postCode, String city){
  202. String addingSQL = "insert into Customer " + " (FirstName,LastName,CustomerAddress,City,Postcode,Country,Email,Password)"
  203. + " values ('" + fname + "', '" + lname + "', '" + Address + "', '" + city + "', '" + postCode + "', '" + Country + "', '" + email + "', '" + password + "')";
  204. try {System.out.println("GOING TO ADD TO DB");
  205. myStmt.execute(addingSQL);
  206. System.out.println("GOING TO ADD TO DB");
  207. } catch (SQLException e) {
  208. e.printStackTrace();
  209. System.out.println("--------- SOMETHIGN WRONG IN ADD QUERY");
  210. }
  211.  
  212.  
  213.  
  214.  
  215. /* String FirstName, String LastName, String UserName, String Password, String StaffType) {
  216. try {
  217. String sql = "insert into Staff " + " (FirstName,LastName,UserName,Password,StaffType)" +
  218. " values ('" + FirstName + "', '" + LastName + "','" + UserName + "','" + getMD5(Password) + "','" + StaffType + "')";
  219. myStmt.executeUpdate(sql);
  220. System.out.println("Inserted");*/
  221. }
  222.  
  223. public List RoomCheck1(String column, String roomSize) {
  224. List rowValues = null;
  225. try {
  226. rowValues = new ArrayList();
  227. myRs = null;
  228. myRs = myStmt.executeQuery("SELECT RoomID FROM Room where RoomSize = \"" + roomSize + "\"");
  229. while (myRs.next()) {
  230. rowValues.add(myRs.getString(1));
  231. }
  232. // for (int i = 0; i < rowValues.size(); i++) {
  233. // System.out.println(rowValues.get(i));
  234. // }
  235. } catch (SQLException e) {
  236. e.printStackTrace();
  237. }
  238. return rowValues;
  239. }
  240.  
  241. public void confirmPaymentAndBook(String issueda, String cardHolder, String cardType, String cardNumberId, String expiryDate, String issueNumber, String ccvcardnumber, String checkInDate, String checkOutDate, String typeOfRoom) {
  242.  
  243.  
  244. String addingSQL = "insert into Payment " + " (CardNumber,ExpiryDate,CardCCV,CardHolderName,CardType,IssueNumber,IssueDate)"
  245. + " values ('" + cardNumberId + "', '" + expiryDate + "', '" + ccvcardnumber + "', '" + cardHolder + "', '" + cardType + "', '" + issueNumber + "', '" + issueda + "')";
  246. try {System.out.println("GOING TO ADD CARD TO DB");
  247. myStmt.execute(addingSQL);
  248. System.out.println("ADDED CARD TO DB");
  249. } catch (SQLException e) {
  250. e.printStackTrace();
  251. System.out.println("--------- SOMETHIGN WRONG IN ADD QUERY");
  252. }
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement