Advertisement
Guest User

restaurantdao

a guest
Feb 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. package cityout2oParadoteo;
  2.  
  3. import java.sql.*;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6.  
  7. public class RestaurantDAO {
  8.  
  9. String errorMessages = "";
  10. PreparedStatement stmt1 = null;
  11. PreparedStatement stmt2 = null;
  12. String sql1 = "select * from Restaurant where id_cat=? and id_area=?";
  13. String sql2 = "select * from Restaurant where id_res=?";
  14. ResultSet rs1 = null;
  15. ResultSet rs2 = null;
  16.  
  17. List<Restaurant> restaurants = new ArrayList<Restaurant>();
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. public static void main (String[] args) {
  27. RestaurantDAO rd = new RestaurantDAO();
  28. try {
  29.  
  30.  
  31. Restaurant restaurant = rd.getRestaurantInfoById(1);
  32.  
  33. } catch (Exception e) {
  34. System.out.println("lol");
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41. /**
  42. * Default constructor
  43. */
  44. public RestaurantDAO() { }
  45.  
  46.  
  47. public List<Restaurant> getRestaurantsByChose(int catid, int areaid) throws Exception {
  48.  
  49. Connection con = null;
  50. CityoutDB db = new CityoutDB();
  51.  
  52. try {
  53. db.open();
  54. con = db.getConnection();
  55.  
  56. stmt1 = con.prepareStatement(sql1);
  57. stmt1.setInt(1,catid);
  58. stmt1.setInt(2,areaid);
  59. rs1 = stmt1.executeQuery();
  60.  
  61. if (rs1.next() == false) {
  62. errorMessages = "Den iparxoun kataxorimena katastimata tis epilogis sas";
  63. throw new SQLException(errorMessages);
  64. }
  65. rs1.previous();
  66.  
  67. while(rs1.next()) {
  68. Restaurant restaurant = new Restaurant(rs1.getInt("id_res"), rs1.getString("name_res"),rs1.getString("address"),rs1.getInt("phone_res"),rs1.getString("photo_url"),rs1.getString("map_res"),rs1.getInt("id_cat"),rs1.getInt("id_area"));
  69. restaurants.add(restaurant);
  70.  
  71. }
  72.  
  73. rs1.close();
  74. stmt1.close();
  75. con.close();
  76.  
  77. return restaurants;
  78. }catch (Exception e) {
  79.  
  80. throw new Exception( e.getMessage() );
  81.  
  82. } finally {
  83.  
  84. try {
  85. db.close();
  86. } catch (Exception e) {
  87. //no need to do anything...
  88. }
  89.  
  90. }
  91.  
  92. } //End of getRestaurantsByChose
  93.  
  94.  
  95.  
  96. public Restaurant getRestaurantInfoById(int idres) throws Exception {
  97.  
  98. System.out.println("lrr");
  99.  
  100. Connection con = null;
  101. CityoutDB db = new CityoutDB();
  102. Restaurant restaurant = null;
  103.  
  104. try {
  105. db.open();
  106. con = db.getConnection();
  107. System.out.println("lrr");
  108. stmt2 = con.prepareStatement(sql2);System.out.println("lrr");
  109. stmt2.setInt(1,idres);System.out.println("lrr");
  110. rs2 = stmt2.executeQuery();System.out.println("lrr");
  111.  
  112.  
  113.  
  114. while(rs2.next()) { System.out.println("ola kala");
  115.  
  116. restaurant = new Restaurant(rs2.getInt("id_res"), rs2.getString("name_res"),rs2.getString("address"),rs2.getInt("phone_res"),rs2.getString("photo_url"),rs2.getString("map_res"),rs2.getInt("id_cat"),rs2.getInt("id_area"));
  117. System.out.println("ola kala");
  118. }
  119.  
  120. rs2.close();
  121. stmt2.close();
  122. con.close();
  123.  
  124. return restaurant;
  125. }catch (Exception e) {
  126.  
  127. throw new Exception( e.getMessage() );
  128.  
  129. } finally {
  130.  
  131. try {
  132. db.close();
  133. } catch (Exception e) {
  134. //no need to do anything...
  135. }
  136.  
  137. }
  138.  
  139.  
  140. } //End of getRestaurantsByChose
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. } //End of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement