Advertisement
Guest User

jdbc

a guest
Mar 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package blockPuzzle.service;
  2.  
  3. import blockPuzzle.entity.Comment;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class CommentServiceJDBC implements CommentService{
  10.     public static final String URL = "jdbc:postgresql://localhost/gamestudio2018";
  11.     public static final String USER = "postgres";
  12.     public static final String PASS = "postgres";
  13.  
  14.     public static final String INSERT =
  15.             "INSERT INTO comment (game, username, comment_text, commented_on) VALUES (?, ?, ?, ?)";
  16.  
  17.  
  18.     @Override
  19.     public void addComment(Comment comment) throws CommentException {
  20.         try {
  21.             Class.forName("org.postgresql.Driver");
  22.         }catch (ClassNotFoundException e) {
  23.             System.err.println(e);
  24.             System.exit(-1);
  25.         }
  26.         try (Connection c = DriverManager.getConnection(URL,USER,PASS)){
  27.             try (PreparedStatement ps = c.prepareStatement(INSERT)){
  28.                 ps.setString(1,comment.getGame());
  29.                 ps.setString(2,comment.getUsername());
  30.                 ps.setString(3,comment.getComment_text());
  31.                 ps.setTimestamp(4,new Timestamp(comment.getCommentedOn().getTime()));
  32.             }catch (SQLException e){
  33.                 throw new CommentException("Error saving comment",e);
  34.             }
  35.         }catch (SQLException e){
  36.             throw new CommentException("Error connecting to database",e);
  37.         }
  38.     }
  39.  
  40.     public static final String SELECT =
  41.             "SELECT game, username, comment_text, commented_on FROM comment " +
  42.                     "WHERE game = ?";
  43.  
  44.     @Override
  45.     public List<Comment> getComments(String game) throws CommentException {
  46.         List<Comment> comments = new ArrayList<>();
  47.  
  48.         try (Connection c = DriverManager.getConnection(URL,USER,PASS)){
  49.             try (PreparedStatement ps = c.prepareStatement(SELECT)) {
  50.                 ps.setString(1,game);
  51.  
  52.                 ResultSet rs = ps.executeQuery();
  53.                 while(rs.next()){
  54.                     Comment cm = new Comment(
  55.                             rs.getString(1),
  56.                             rs.getString(2),
  57.                             rs.getString(3),
  58.                             rs.getTimestamp(4)
  59.                     );
  60.                     comments.add(cm);
  61.                 }
  62.             }catch (SQLException e) {
  63.                 throw new CommentException("Error saving comment",e);
  64.             }
  65.         }catch (SQLException e){
  66.             throw new CommentException("Error connecting to database",e);
  67.         }
  68.         return comments;
  69.     }
  70.  
  71.  
  72.     public static void main(String[] args) throws Exception{
  73.         Comment comment = new Comment("blockpuzzle","jakub","Vyborna hra nemam co dodat", new java.util.Date());
  74.         CommentService commentService = new CommentServiceJDBC();
  75.         commentService.addComment(comment);
  76.         System.out.println(commentService.getComments("blockpuzzle"));
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement