Advertisement
Guest User

Untitled

a guest
Mar 21st, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package sk.tuke.gamestudio.bejeweled.service;
  2.  
  3. import sk.tuke.gamestudio.bejeweled.entity.Comment;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. /**
  10. * Created by miniBOSS on 21. 3. 2016.
  11. */
  12. public class CommentServiceImpl implements CommentService {
  13. private static final String URL = "jdbc:oracle:thin:@oracle.kpi.fei.tuke.sk:1521:db11g";
  14. private static final String LOGIN = "jozefperhac";
  15. private static final String PASSWORD = "jozefperhac";
  16.  
  17. private static final String INSERT_STMT =
  18. "INSERT INTO GAMETABLE (player, game, comment) VALUES (?, ?, ?)";
  19.  
  20. private static final String SELECT_STMT =
  21. "SELECT comment FROM GAMETABLE WHERE game = ? ORDER BY game DESC";
  22.  
  23. @Override
  24. public void addComment(Comment comment) throws CommentException {
  25. try(Connection connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
  26. PreparedStatement ps = connection.prepareStatement(INSERT_STMT)) {
  27. ps.setString(1, comment.getPlayer());
  28. ps.setString(2, comment.getGame());
  29. ps.setString(3, comment.getComment());
  30. ps.executeUpdate();
  31. } catch (SQLException e) {
  32. throw new CommentException("Error saving comment", e);
  33. }
  34. }
  35.  
  36. @Override
  37. public List<Comment> getCommentForGame(String game) throws CommentException {
  38. List<Comment> comments = new ArrayList<>();
  39. try(Connection connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
  40. PreparedStatement ps = connection.prepareStatement(SELECT_STMT)) {
  41. ps.setString(1, game);
  42. try(ResultSet rs = ps.executeQuery()) {
  43. while(rs.next()) {
  44. Comment comment = new Comment(rs.getString(1), rs.getString(2),
  45. rs.getString(3));
  46. comments.add(comment);
  47. }
  48. }
  49. } catch (SQLException e) {
  50. throw new CommentException("Error loading comment", e);
  51. }
  52. return comments;
  53. }
  54.  
  55. public static void main(String[] args) throws CommentException {
  56. Comment comment = new Comment("jaro", "bejeweled", "Dobra hra...");
  57. CommentService commentService = new CommentServiceImpl();
  58. commentService.addComment(comment);
  59. //System.out.println(commentService.getCommentForGame("bejeweled"));
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement