Advertisement
Guest User

getCodeTypes

a guest
Dec 8th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. public static ArrayList<CodeType> getCodeTypes() {
  2. ArrayList<CodeType> categories = new ArrayList();
  3.  
  4. String database = "catchphrase";
  5. PreparedStatement ps = null;
  6. String sql = null;
  7. Connection conn = null;
  8.  
  9. try {
  10.  
  11. //connect to database
  12. String propFileName = "spring.data-access";
  13. ResourceBundle rb = ResourceBundle.getBundle(propFileName);
  14. String dbUserName = rb.getString("jdbc.username");
  15. String dbPassword = rb.getString("jdbc.password");
  16. System.out.println("BJM-Set the database to "+database);
  17.  
  18. DatabaseConnection newConnection = new DatabaseConnection(database, dbUserName, dbPassword);
  19. conn = ConnectionUtils.getConnection(newConnection);
  20.  
  21. sql = "SELECT * FROM `CodeType` order by codeTypeId";
  22.  
  23. ps = conn.prepareStatement(sql);
  24. ResultSet rs = ps.executeQuery();
  25. while (rs.next()) {
  26. // It is possible to get the columns via name
  27. // also possible to get the columns via the column number
  28. // which starts at 1
  29.  
  30. // e.g. resultSet.getString(2);
  31. CodeType category = new CodeType();
  32. category.setCodeTypeId(rs.getInt("codeTypeId"));
  33. category.setEnglishDescription(rs.getString("englishDescription"));
  34. category.setFrenchDescription(rs.getString("frenchDescription"));
  35. System.out.println("Found code type=" + category);
  36. categories.add(category);
  37. }
  38. } catch (Exception e) {
  39. String errorMessage = e.getMessage();
  40. e.printStackTrace();
  41. } finally {
  42. DbUtils.close(ps, conn);
  43. }
  44.  
  45. return categories;
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement