Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package org.elsys.ip.rest.repository;
  2.  
  3. import org.elsys.ip.rest.model.Country;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class CountryRepositoryJDBC {
  10. private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  11. private static final String DB_URL = "jdbc:mysql://localhost/country"; //change
  12.  
  13. //Database credentials
  14. private static final String USER = "root";
  15. private static final String PASS = "root";
  16.  
  17. public List<Country> getCountryList () {
  18. List<Country> countries = new ArrayList<>();
  19. Connection conn = null;
  20.  
  21. try {
  22. Class.forName(JDBC_DRIVER);
  23. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  24.  
  25. Statement stmt = null;
  26. ResultSet rs = null;
  27.  
  28. try {
  29. stmt = conn.createStatement();
  30. String sql = "SELECT id, name FROM country";
  31. rs = stmt.executeQuery(sql);
  32.  
  33. while (rs.next()) {
  34. Country country = new Country();
  35. country.setId(rs.getInt("id"));
  36. country.setName(rs.getString("name"));
  37. countries.add(country);
  38. }
  39. } finally {
  40. if (stmt != null && !stmt.isClosed()) {
  41. stmt.close();
  42. }
  43.  
  44. if (rs != null && !rs.isClosed()) {
  45. rs.close();
  46. }
  47. }
  48.  
  49. } catch (ClassNotFoundException | SQLException e) {
  50. e.printStackTrace();
  51. }
  52.  
  53. return countries;
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement