Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. package brain;
  2.  
  3. import data.DBConnector;
  4. import java.awt.print.Book;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11.  
  12. /**
  13. *
  14. * @author vfgya_000
  15. */
  16. public class DAO {
  17.  
  18. private Connection con;
  19.  
  20. public DAO() {
  21. con = new DBConnector().getConnection();
  22. }
  23.  
  24. DBConnector db = new DBConnector();
  25.  
  26. public User getUser(String name, String pword) {
  27. Statement stmt = null;
  28. String query = "select * from user where USERNAME = '" + name + "' and Password = '" + pword + "'";
  29. User user = null;
  30.  
  31. try {
  32. stmt = con.createStatement();
  33. ResultSet rs = stmt.executeQuery(query);
  34.  
  35. if (rs.next()) {
  36. user = new User(
  37. rs.getInt(1),
  38. rs.getString(2),
  39. rs.getString(3),
  40. rs.getInt(4));
  41. }
  42. } catch (SQLException e) {
  43. System.out.println(e.getMessage());
  44.  
  45. }
  46.  
  47. return user;
  48. }
  49.  
  50. public ArrayList<Bottom> getBottoms() {
  51. ArrayList<Bottom> bottoms = new ArrayList<Bottom>();
  52.  
  53. Statement stmt = null;
  54. String query = "select * from bottoms";
  55. Bottom bottom = null;
  56.  
  57. try {
  58. stmt = con.createStatement();
  59. ResultSet rs = stmt.executeQuery(query);
  60.  
  61. while (rs.next()) {
  62. int btid = rs.getInt("bID");
  63. String bttype = rs.getString("bType");
  64. int btprice = rs.getInt("bPrice");
  65. bottom = new Bottom(btid, bttype, btprice);
  66. bottoms.add(bottom);
  67. }
  68.  
  69. } catch (SQLException e) {
  70. System.out.println(e.getMessage());
  71. }
  72.  
  73. return bottoms;
  74.  
  75. }
  76.  
  77. public ArrayList<Topping> getToppings() {
  78. ArrayList<Topping> toppings = new ArrayList<Topping>();
  79.  
  80. Statement stmt = null;
  81. String query = "select * from toppings";
  82. Topping topping = null;
  83.  
  84. try {
  85. stmt = con.createStatement();
  86. ResultSet rs = stmt.executeQuery(query);
  87.  
  88. while (rs.next()) {
  89. int tpid = rs.getInt("tID");
  90. String tptype = rs.getString("tType");
  91. int tpprice = rs.getInt("tPrice");
  92. topping = new Topping(tpid, tptype, tpprice);
  93. toppings.add(topping);
  94. }
  95.  
  96. } catch (SQLException e) {
  97. System.out.println(e.getMessage());
  98. }
  99.  
  100. return toppings;
  101.  
  102. }
  103.  
  104. public Bottom getBottomByType(String type) {
  105. Statement stmt = null;
  106. String query = "select * from bottoms where bType = " + type;
  107. Bottom bottom = null;
  108.  
  109. try {
  110. stmt = con.createStatement();
  111. ResultSet rs = stmt.executeQuery(query);
  112.  
  113. if (rs.next()) {
  114. bottom = new Bottom(
  115. rs.getInt(1),
  116. rs.getString(2),
  117. rs.getInt(3)
  118. );
  119. }
  120. } catch (SQLException e) {
  121. System.out.println(e.getMessage());
  122. }
  123. return bottom;
  124. }
  125.  
  126. public Topping getToppingByType(String type) {
  127. Statement stmt = null;
  128. String query = "select * from toppings where tType = " + type;
  129. Topping topping = null;
  130.  
  131. try {
  132. stmt = con.createStatement();
  133. ResultSet rs = stmt.executeQuery(query);
  134.  
  135. if (rs.next()) {
  136. topping = new Topping(
  137. rs.getInt(1),
  138. rs.getString(2),
  139. rs.getInt(3)
  140. );
  141. }
  142. } catch (SQLException e) {
  143. System.out.println(e.getMessage());
  144. }
  145. return topping;
  146. }
  147. public void insertOrder(int userid, String bottom, String topping, int price) {
  148. PreparedStatement insertRS = null;
  149.  
  150. String stmtRS = "INSERT INTO orders (fkuserid, bttype, tptype, price) VALUES (?,?,?,?)";
  151. try {
  152.  
  153. insertRS = con.prepareStatement(stmtRS);
  154.  
  155.  
  156. insertRS.setInt(1, userid);
  157. insertRS.setString(2, bottom);
  158. insertRS.setString(3, topping);
  159. insertRS.setInt(4, price);
  160.  
  161. insertRS.executeUpdate();
  162.  
  163.  
  164. } catch (Exception e)
  165. {
  166. if(con != null) {
  167. try {
  168. System.out.println(e.getMessage());
  169. con.rollback();
  170. }
  171. catch (SQLException excep) {
  172.  
  173. }
  174. }
  175. }
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement