Advertisement
Guest User

CourseQueries

a guest
Nov 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package planningtool;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10.  
  11. public class courseQueries {
  12. /*
  13. *
  14. *
  15. * This Class manages database operations associated to the Course Class
  16. * Don't worry yet about the try/catch/finally blocks in some methods. This will be explained later in the course
  17. */
  18. // DB connection details
  19. private static final String URL = "jdbc:mysql://eu-cdbr-azure-west-b.cloudapp.net:3306/inkinen_jesse";
  20. private static final String USERNAME = "b9b0dcc9056907";
  21. private static final String PASSWORD = "0d8483fc";
  22.  
  23. private Connection connection = null;
  24. private PreparedStatement selectAllCourses = null;
  25. private PreparedStatement insertCourse = null;
  26.  
  27. public courseQueries()
  28. {
  29. try
  30. {
  31. connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // Starts a connection to the database
  32. selectAllCourses = connection.prepareStatement("SELECT * FROM courses"); // Prepare the select query that gets all Courses from the database
  33.  
  34. // Here you will need to prepare the insert query
  35. insertCourse = connection.prepareStatement("INSERT INTO courses VALUES (?,?,?,?)");
  36. }
  37. catch (SQLException sqlException)
  38. {
  39. sqlException.printStackTrace();
  40. System.exit(1);
  41. }
  42. }
  43.  
  44. /*
  45. * This method will execute the select query that gets all Courses from the database.
  46. * It returns an ArrayList containing Course objects initialized with Course data from each row in the Courses table (database)
  47. */
  48. public ArrayList<Course> getAllCourses()
  49. {
  50. ArrayList<Course> results = null;
  51. ResultSet resultSet = null;
  52.  
  53. try
  54. {
  55. resultSet = selectAllCourses.executeQuery(); // Here is where we actually execute the select query. resultSet contains the rows returned by the query
  56. results = new ArrayList<Course>();
  57.  
  58. while(resultSet.next()) // for each row returned by the select query...
  59. {
  60. // Initialize a new Course object with the row's data. Add the Course object to the results ArrayList
  61. results.add(new Course(
  62. resultSet.getString("plateNr"), // get the value associated to the platNr column
  63. resultSet.getString("colour"), // get the value associated to the colour column
  64. resultSet.getString("model"), // get the value associated to the model column
  65. resultSet.getInt("year"))); // get the value associated to the year column
  66. }
  67. } // end try
  68. catch (SQLException sqlException)
  69. {
  70. sqlException.printStackTrace();
  71. }
  72. finally
  73. {
  74. try
  75. {
  76. resultSet.close();
  77. }
  78. catch (SQLException sqlException)
  79. {
  80. sqlException.printStackTrace();
  81. }
  82. } // end finally
  83.  
  84. return results;
  85. } // end method getAllCourses
  86.  
  87. /*
  88. * Method that inserts a new Course in the database
  89. */
  90. public void addCourse(String plateNr, String colour, String model, int modelYear)
  91. {
  92. try
  93. {
  94. // Setting the values for the question marks '?' in the prepared statement
  95. insertCourse.setString(1, plateNr);
  96. insertCourse.setString(2, colour);
  97. insertCourse.setString(3, model);
  98. insertCourse.setInt(4, modelYear);
  99.  
  100. // result will contain the amount of updated rows. It should be 1. To simplify the code let's not verify this
  101. int result = insertCourse.executeUpdate();
  102. }
  103. catch (SQLException sqlException)
  104. {
  105. sqlException.printStackTrace();
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement