Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. package io;
  2. import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
  3. import static dk.au.hum.imv.persistence.db.JDBCConnectionFactory.*;
  4. import domain.*;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import domain.Post;
  9.  
  10. import java.sql.*;
  11.  
  12. import java.sql.Connection;
  13.  
  14. public class PostMapper {
  15. private static final String URL = "jdbc:mysql://mysql.student.hum.au.dk/ukko"; //going to the server
  16. private static final String USERNAME = "ukko";
  17. private static final String PASSWORD = "FQW9AA8N";
  18. private static final String dbDriver = "com.mysql.jdbc.Driver";
  19. private static final String TABLE_NAME = "car";
  20.  
  21. private Connection myConnection; //put Connection into the variable myConnection
  22. private PreparedStatement selectAllCar;
  23.  
  24.  
  25.  
  26. public PostMapper() { //constructor
  27. JDBCConnectionFactory.initManualConnectionHandling(URL, USERNAME, PASSWORD, dbDriver);
  28.  
  29. String brands = "'Lada','Sherp', 'AvtoRos', 'Marussia Motors', 'GAZ', 'Sofia', 'Bulgaralpine', 'UAZ', 'Liebherr'";
  30.  
  31. try{
  32. myConnection = getInstance().getNewConnection();
  33. String sqlStatement = "SELECT * FROM " + TABLE_NAME;
  34. if (brands != null && !"".equals(brands)) sqlStatement += " WHERE brand IN (" + brands + ")";
  35. // OBERED BY year ASC DESC
  36. // sqlStatement += " OBERED BY year ASC";
  37. selectAllCar = myConnection.prepareStatement(sqlStatement); //selecting the whole table
  38.  
  39. }
  40. catch (SQLException sqlException){
  41. sqlException.printStackTrace();
  42. System.exit(1);
  43. }
  44.  
  45. }
  46. public ArrayList<Post> getAllCars() { //grab information and put in an array
  47.  
  48. ArrayList<Post> results = new ArrayList<Post>();
  49. ResultSet resultSet = null;
  50.  
  51. try {
  52. resultSet = selectAllCar.executeQuery();
  53. //results = new ArrayList<Post>(); //actually putting in the array
  54.  
  55. while (resultSet.next()) {
  56.  
  57. results.add(new domain.Post(
  58. resultSet.getString(TABLE_NAME + ".model"),
  59. resultSet.getString(TABLE_NAME + ".brand"),
  60. resultSet.getString(TABLE_NAME + ".year"),
  61. resultSet.getString(TABLE_NAME + ".content"),
  62. resultSet.getLong(TABLE_NAME + ".id")));
  63. }
  64. } catch (SQLException sqlException) {
  65. sqlException.printStackTrace();
  66. } finally {
  67. if (resultSet != null)
  68. try {
  69. resultSet.close();
  70. } catch (SQLException sqlException) {
  71. sqlException.printStackTrace();
  72. close();
  73. }
  74. }
  75. return results;
  76. }
  77.  
  78.  
  79. public void close(){
  80. try {
  81. myConnection.close();
  82. }
  83. catch(SQLException sqlException){
  84. sqlException.printStackTrace();
  85. close();
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement