Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. package jdbc_gui;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10.  
  11.  
  12. public class DatabaseController {
  13. // These connection setting will work on the lab computers
  14. // If you use a different database, feel free to change these settings!
  15. private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
  16. private static final String DB_USER = "hr";
  17. private static final String DB_PW = "hr";
  18.  
  19. public static boolean testDriver()
  20. {
  21. // Code to test the driver is loaded
  22. try
  23. {
  24. Class.forName("oracle.jdbc.driver.OracleDriver");
  25. }
  26. catch (ClassNotFoundException e)
  27. {
  28. // Display an error message on the GUI if the driver is not loaded
  29. ProductGUI.displayError("Missing Oracle JDBC Driver?");
  30. return false;
  31. }
  32.  
  33. ProductGUI.displayMessage("JDBC Driver Loaded!");
  34. return true;
  35. }
  36.  
  37. public static Connection getConnection()
  38. {
  39. try
  40. {
  41. return DriverManager.getConnection(DB_URL, DB_USER, DB_PW);
  42. }
  43. catch(SQLException e)
  44. {
  45. ProductGUI.displayError("Invalid Database Connection parameters!");
  46. return null;
  47. }
  48. }
  49.  
  50. public static ArrayList<ProductCategory> getCategories() throws SQLException
  51. {
  52. Connection connection = getConnection();
  53. Statement statement = connection.createStatement();
  54.  
  55. String sqlCommand = "SELECT * FROM ProductCategory";
  56. ResultSet rset = statement.executeQuery(sqlCommand);
  57.  
  58. ArrayList<ProductCategory> categoryList = new ArrayList<>();
  59. while(rset.next())
  60. {
  61. int id = rset.getInt("PRODUCTCATEGORYID");
  62. String name = rset.getString("PRODUCTCATEGORYNAME");
  63.  
  64. categoryList.add(new ProductCategory(id, name));
  65. }
  66.  
  67. if (statement != null)
  68. statement.close();
  69. if (connection != null)
  70. connection.close();
  71.  
  72. return categoryList;
  73. }
  74.  
  75. public static ArrayList<Product> getProducts() throws SQLException
  76. {
  77. ArrayList<Product> productList = new ArrayList<Product>();
  78.  
  79. // TODO 1 - 1- Read all product from database using a prepared statement
  80. // 2- From the resultset, instanciate products, add in the array
  81.  
  82.  
  83. // productList.add(new Product(1, 1, "test", "test desc", 10, 12.12));
  84. Connection dbConnection = getConnection();
  85. Statement statement = dbConnection.createStatement();
  86.  
  87. ResultSet rs = statement.executeQuery("SELECT * FROM product");
  88.  
  89. while(rs.next()){
  90. int productId = rs.getInt("PRODUCTID");
  91. int productCatId = rs.getInt("PRODUCTCATEGORYID");
  92. String prodName = rs.getString("PRODUCTNAME");
  93. String prodDesc = rs.getString("PRODUCTDESCRIPTION");
  94. int prodInv = rs.getInt("PRODUCTINVENTORYAMOUNT");
  95. double price = rs.getDouble("PRODUCTPRICE");
  96.  
  97. productList.add(new Product(productId, productCatId, prodName, prodDesc, prodInv, price));
  98. }
  99.  
  100. return productList;
  101. }
  102.  
  103. public static void insertProduct(Product product) throws SQLException
  104. {
  105. Connection dbConnection = null;
  106. PreparedStatement statement = null;
  107. //ArrayList<Product> productList = new ArrayList<Product>();
  108. String insertTableSQL = "INSERT INTO product"
  109. + "(productid, productcategoryid, productname, productdescription, productinventoryamount, productprice) VALUES"
  110. + "(?,?,?,?,?,?)";
  111.  
  112. try {
  113. dbConnection = getConnection();
  114. statement = dbConnection.prepareStatement(insertTableSQL);
  115.  
  116. statement.setInt(1, product.getId());
  117. statement.setInt(2, product.getCategoryId());
  118. statement.setString(3, product.getName());
  119. statement.setString(4, product.getDescription());
  120. statement.setInt(5, product.getInventory());
  121. statement.setDouble(6, product.getPrice());
  122.  
  123. statement.execute();
  124. getProducts();
  125.  
  126. } catch (NumberFormatException e) {
  127. System.out.println(e.getMessage());
  128. }
  129. //productList.add(product);
  130. }
  131.  
  132. public static void updateProduct(Product product) throws SQLException
  133. {
  134. Connection dbConnection = null;
  135. PreparedStatement statement = null;
  136. //ArrayList<Product> productList = new ArrayList<Product>();
  137. String updateTableSQL = "UPDATE product"
  138. + " SET productid = ?, "
  139. + "productcategoryid = ?, "
  140. + "productname = ?,"
  141. + " productdescription = ?,"
  142. + " productinventoryamount = ?,"
  143. + " productprice = ? "
  144. + "WHERE productid = ?";
  145. try {
  146. dbConnection = getConnection();
  147. statement = dbConnection.prepareStatement(updateTableSQL);
  148.  
  149. statement.setInt(1, product.getId());
  150. statement.setInt(2, product.getCategoryId());
  151. statement.setString(3, product.getName());
  152. statement.setString(4, product.getDescription());
  153. statement.setInt(5, product.getInventory());
  154. statement.setDouble(6, product.getPrice());
  155. statement.setInt(7, product.getId());
  156.  
  157. statement.executeUpdate();
  158. getProducts();
  159.  
  160. } catch (NumberFormatException e) {
  161. System.out.println(e.getMessage());
  162. }
  163. // TODO 3 - Run a prepared statement to Update the product in the database
  164. }
  165.  
  166. public static void deleteProduct(int productId) throws SQLException
  167. {
  168. Connection dbConnection = null;
  169. PreparedStatement statement = null;
  170.  
  171. String removeTableSQL = "DELETE product "
  172. +"WHERE productid = ?";
  173. try{
  174. dbConnection = getConnection();
  175. statement = dbConnection.prepareStatement(removeTableSQL);
  176. statement.setInt(1, productId);
  177. statement.execute();
  178. } catch(NumberFormatException e){
  179. System.out.println(e.getMessage());
  180. }
  181. }
  182.  
  183. private static int executeQuery(String select_maXproductid_FROM_product) {
  184. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement