Guest User

Untitled

a guest
Jan 4th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.sql.*;
  3.  
  4. public class JDBCPractice
  5. {
  6. private String selectStatement, reportStatement, updateStatement;
  7. private DatabaseAccess da;
  8. private ResultSet rs;
  9.  
  10. private String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  11. private String jdbc = "jdbc:odbc:JDBC4600";
  12. private String username = "4600";
  13. private String password = "student";
  14.  
  15. private int todaysDate;
  16. double price;
  17. int QOH;
  18. String productName;
  19. int vendorID;
  20. int reorderLevel;
  21.  
  22. /**
  23. * Constructor just sets the date.
  24. */
  25. public JDBCPractice()
  26. {
  27. todaysDate = 20081126;
  28. }
  29.  
  30.  
  31. /**
  32. * Return the Status Report for one Customer. Includes CustomerID, CustomerName,
  33. * LateFees and a List of Movies Currently Rented to that Customer.
  34. *
  35. */
  36. public void customerStatusReport(int custID)
  37. {
  38. System.out.println();
  39. System.out.println("Customer Status Report");
  40.  
  41.  
  42. /** Connect to the database
  43. */
  44. DatabaseAccess da = new DatabaseAccess();
  45. da.loadDriver(driver);
  46. da.connectDatabase(jdbc,username,password);
  47.  
  48. /** Write the SQL, execute it, and print contents of resultSet
  49. */
  50. selectStatement = "Select CustomerID, CustomerName, LateFees FROM Customers where CustomerID = " + custID;
  51. rs = da.executeSQL(selectStatement);
  52.  
  53. try
  54. {
  55. while(rs.next())
  56. {
  57. int custName = rs.getInt(2);
  58. double lateFees = rs.getDouble(3);
  59. System.out.println("Customer ID: " + custID + " Customer Name: " + custName);
  60. System.out.println("Outstanding Late Fees: " + lateFees );
  61. }
  62. }
  63. catch(SQLException error)
  64. {
  65. System.err.println("Error Accessing Customer Table: " + error.toString());
  66. System.exit(4);
  67. }
  68.  
  69.  
  70. System.out.println();
  71. System.out.println("Movies Currently Checked Out");
  72. System.out.println("ID Movie Name Due Date Overdue? ");
  73.  
  74.  
  75.  
  76. /** Write the SQL, execute it, and print contents of resultSet
  77. */
  78. //selectStatement = "Select MovieID, MovieName, DueDate, RentedToID from Movie where RentedToID = " + custID;
  79. selectStatement = "Select * from Movie where RentedToID = " + custID;
  80. rs = da.executeSQL(selectStatement);
  81.  
  82.  
  83.  
  84. da.closeDatabase();
  85. }
  86.  
  87. /**
  88. * List all Products in the Database
  89. */
  90. public void listAllProducts()
  91. {
  92. System.out.println();
  93. System.out.println("List Of All Products");
  94. System.out.println("ID Description Price QOH ReorderLevel etc.");
  95.  
  96. /** Connect to the database
  97. */
  98. DatabaseAccess da = new DatabaseAccess();
  99. da.loadDriver(driver);
  100. da.connectDatabase(jdbc,username,password);
  101.  
  102. /** Write the SQL, execute it, and print contents of resultSet
  103. */
  104. selectStatement = "Select * from Inventory";
  105. rs = da.executeSQL(selectStatement);
  106. da.reportSQL(rs);
  107. da.closeDatabase();
  108. }
  109.  
  110. public void listAllMovies()
  111. {
  112. System.out.println();
  113. System.out.println("List Of All Movies");
  114. System.out.println("ID Title Due Date RenterID Price ");
  115.  
  116. /** Connect to the database
  117. */
  118. DatabaseAccess test = new DatabaseAccess();
  119. test.loadDriver(driver);
  120. test.connectDatabase(jdbc,username,password);
  121.  
  122. selectStatement = "SELECT * FROM MOVIE";
  123. rs = test.executeSQL(selectStatement);
  124. test.reportSQL(rs);
  125. test.closeDatabase();
  126. }
  127.  
  128. public void listAllFieldsForMoviesForOneCustomer(int custID)
  129. {
  130. System.out.println();
  131. System.out.println("List Of All Movie Fields for one Customer");
  132. System.out.println("ID Movie Name Due Date etc. ");
  133.  
  134. /** Connect to the database
  135. */
  136. DatabaseAccess da = new DatabaseAccess();
  137. da.loadDriver(driver);
  138. da.connectDatabase(jdbc,username,password);
  139.  
  140. /** Write the SQL, execute it, and print contents of resultSet
  141. */
  142. //selectStatement = "Select MovieID, MovieName, DueDate, RentedToID from Movie where RentedToID = " + custID;
  143. selectStatement = "Select * from Movie where RentedToID = " + custID;
  144. rs = da.executeSQL(selectStatement);
  145.  
  146. da.reportSQL(rs);
  147. da.closeDatabase();
  148. }
  149.  
  150.  
  151. public void listMovieNamesDueDatesForOneCustomer(int custID)
  152. {
  153. System.out.println();
  154. System.out.println("List Of All Movie Names and DueDates for one Customer");
  155.  
  156.  
  157. /** Connect to the database
  158. */
  159. DatabaseAccess da = new DatabaseAccess();
  160. da.loadDriver(driver);
  161. da.connectDatabase(jdbc,username,password);
  162.  
  163. /** Write the SQL, execute it, and print contents of resultSet
  164. */
  165. selectStatement = "Select MovieID, MovieName, DueDate from Movie where RentedToID = " + custID;
  166. rs = da.executeSQL(selectStatement);
  167.  
  168. try
  169. {
  170. while(rs.next())
  171. {
  172. String movieName = rs.getString(2);
  173. int dueDate = rs.getInt(3);
  174. System.out.println("Movie Name: " + movieName + " DueDate: " + dueDate);
  175. }
  176. }
  177. catch(SQLException error)
  178. {
  179. System.err.println("Error Accessing Movie Table: " + error.toString());
  180. System.exit(4);
  181. }
  182.  
  183. da.closeDatabase();
  184. }
  185. }
Add Comment
Please, Sign In to add comment