Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. package sk.tuke.gamestudio.service;
  2.  
  3. import sk.tuke.gamestudio.entity.Rating;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9.  
  10. import static sk.tuke.gamestudio.game.slidealama.core.Field.GAME_NAME;
  11.  
  12. public class RatingServiceJDBC implements RatingService{
  13.  
  14. public static final String URL = "jdbc:postgresql://localhost/gamestudio";
  15. public static final String USER = "postgres";
  16. public static final String PASSWORD = "boston";
  17.  
  18. public static final String INSERT_RATING =
  19. "INSERT INTO rating (player,game, ratings, playedon) VALUES (?, ?, ?, ?)";
  20.  
  21. public static final String SELECT_RATING =
  22. "SELECT player, game, ratings, playedon FROM rating WHERE game = ? ORDER BY ratings DESC LIMIT 10;";
  23.  
  24. public static final String SELECT_AVG =
  25. "SELECT avg(ratings) FROM rating";
  26.  
  27. public static final String SELECT_RATE2 =
  28. "SELECT ratings FROM rating\n"
  29. + "where player = ?";
  30.  
  31.  
  32. @Override
  33. public void setRating(Rating rating) throws RatingException {
  34. try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
  35. try(PreparedStatement ps = connection.prepareStatement(INSERT_RATING)){
  36. ps.setString(1, rating.getPlayer());
  37. ps.setString(2, rating.getGame());
  38. ps.setInt(3, rating.getRating());
  39. ps.setDate(4, new java.sql.Date(rating.getRatedon().getTime()));
  40. ps.executeUpdate();
  41. }
  42. } catch (SQLException e) {
  43. throw new RatingException("Error saving score", e);
  44. }
  45. }
  46.  
  47. @Override
  48. public int getAverageRating(String game) throws RatingException {
  49. int result = 0;
  50. try(Connection connection=DriverManager.getConnection(URL,USER, PASSWORD);
  51. Statement stm = connection.createStatement();
  52. ResultSet rs = stm.executeQuery(SELECT_AVG)) {
  53. rs.next();
  54. result = rs.getInt(1);
  55.  
  56. } catch (SQLException e) {
  57. throw new RatingException(e.getMessage());
  58. }
  59. return result;
  60. }
  61.  
  62.  
  63. @Override
  64. public int getRating(String game, String player) throws RatingException {
  65.  
  66. int result = 0;
  67. ResultSet rs = null;
  68. try(Connection connection =
  69. DriverManager.getConnection(URL, USER, PASSWORD);
  70. PreparedStatement stm = connection.prepareStatement(SELECT_RATE2)) {
  71. stm.setString(1, player);
  72. rs = stm.executeQuery();
  73. if(rs.next())
  74. result = rs.getInt(1);
  75.  
  76. } catch (SQLException e) {
  77. throw new RatingException(e.getMessage());
  78. }
  79. return result;
  80. }
  81.  
  82.  
  83.  
  84. @Override
  85. public List<Rating> getRatings(String game) throws ScoreException {
  86. List<Rating>ratings = new ArrayList<>();
  87. try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
  88. try(PreparedStatement ps = connection.prepareStatement(SELECT_RATING)){
  89. ps.setString(1, game);
  90. try(ResultSet rs = ps.executeQuery()) {
  91. while(rs.next()) {
  92. Rating rating = new Rating(
  93. rs.getString(1),
  94. rs.getString(2),
  95. rs.getInt(3),
  96. rs.getTimestamp(4)
  97. );
  98. ratings.add(rating);
  99. }
  100. }
  101. }
  102. } catch (SQLException e) {
  103. throw new ScoreException("Error loading score", e);
  104. }
  105. return ratings;
  106. }
  107.  
  108. public static void main(String[] args) throws Exception {
  109. Rating rating=new Rating("vlado",GAME_NAME,5,new Date());
  110. RatingService ratingService=new RatingServiceJDBC();
  111. ratingService.setRating(rating);
  112. System.out.println(ratingService.getRatings(GAME_NAME));
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement