Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gamestudio.Service;
- import gamestudio.entity.Comment;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Date;
- /**
- * Created by Marek on 24. 3. 2016.
- */
- public class CommentServiceImpl implements CommentService {
- private static final String URL = "jdbc:oracle:thin:@oracle.kpi.fei.tuke.sk:1521:db11g";
- private static final String LOGIN = "marekkristof";
- private static final String PASSWORD = "marek";
- private static final String INSERT_STMT =
- "INSERT INTO COMMENT (PLAYER, GAME, COMMENT, PLAYEDON) VALUES (?, ?, ?, ?)";
- private static final String SELECT_STMT =
- "SELECT PLAYER, GAME, COMMENT, PLAYEDON FROM COMMENT";
- @Override
- public void addComment(Comment comment) throws CommentException {
- try(Connection connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
- PreparedStatement ps = connection.prepareStatement(INSERT_STMT)) {
- ps.setString(1, comment.getPlayer());
- ps.setString(2, comment.getGame());
- ps.setString(3, comment.getText());
- ps.setDate(4, new java.sql.Date(comment.getPlayedOn().getTime()));
- ps.executeUpdate();
- } catch (SQLException e) {
- throw new CommentException("Error saving comment", e);
- }
- }
- @Override
- public List<Comment> getComments(String game) throws CommentException {
- List<Comment> comments = new ArrayList<>();
- try(Connection connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
- PreparedStatement ps = connection.prepareStatement(SELECT_STMT)) {
- // ps.setString(2, game);
- try(ResultSet rs = ps.executeQuery()) {
- while(rs.next()) {
- Comment comment = new Comment(rs.getString(1), rs.getString(2),
- rs.getString(3), rs.getDate(4));
- comments.add(comment);
- }
- }
- } catch (SQLException e) {
- throw new CommentException("Error loading comment", e);
- }
- return comments;
- }
- public static void main(String[] args) throws CommentException {
- Comment commment = new Comment("marek", "tiles_hangman", "perfect!!", new Date());
- CommentService commentService = new CommentServiceImpl();
- commentService.addComment(commment);
- // System.out.println(scoreService.getBestScoresForGame("mines"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement