Advertisement
Guest User

Untitled

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