Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sk.tuke.gamestudio.bejeweled.service;
- import sk.tuke.gamestudio.bejeweled.entity.Comment;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by miniBOSS on 21. 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 = "jozefperhac";
- private static final String PASSWORD = "jozefperhac";
- private static final String INSERT_STMT =
- "INSERT INTO GAMETABLE (player, game, comment) VALUES (?, ?, ?)";
- private static final String SELECT_STMT =
- "SELECT comment FROM GAMETABLE WHERE game = ? ORDER BY game DESC";
- @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.getComment());
- ps.executeUpdate();
- } catch (SQLException e) {
- throw new CommentException("Error saving comment", e);
- }
- }
- @Override
- public List<Comment> getCommentForGame(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(1, game);
- try(ResultSet rs = ps.executeQuery()) {
- while(rs.next()) {
- Comment comment = new Comment(rs.getString(1), rs.getString(2),
- rs.getString(3));
- comments.add(comment);
- }
- }
- } catch (SQLException e) {
- throw new CommentException("Error loading comment", e);
- }
- return comments;
- }
- public static void main(String[] args) throws CommentException {
- Comment comment = new Comment("jaro", "bejeweled", "Dobra hra...");
- CommentService commentService = new CommentServiceImpl();
- commentService.addComment(comment);
- //System.out.println(commentService.getCommentForGame("bejeweled"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement