SHARE
TWEET

Untitled

a guest Dec 13th, 2016 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package data;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.Date;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import model.LoginAttempt;
  21. import model.Message;
  22. import model.User;
  23. import util.GuestBookException;
  24.  
  25. /**
  26.  *
  27.  * @author Jasper
  28.  */
  29. public class GuestBookDA {
  30.  
  31.     private static final String URL = "jdbc:mysql://localhost:3333/guestbook";
  32.     private static final String UID = "admin";
  33.     private static final String PWD = "admin";
  34.  
  35.     private Connection connection;
  36.  
  37.     private static GuestBookDA instance;
  38.  
  39.     public static GuestBookDA getInstance() {
  40.         if (instance == null) {
  41.             instance = new GuestBookDA();
  42.         }
  43.  
  44.         return instance;
  45.     }
  46.  
  47.     private GuestBookDA() {
  48.         try {
  49.             Class.forName("com.mysql.jdbc.Driver");
  50.             this.connection = DriverManager.getConnection(URL, UID, PWD);
  51.         } catch (ClassNotFoundException ex) {
  52.             throw new GuestBookException("Unable to load database driver.", ex);
  53.         } catch (SQLException ex) {
  54.             throw new GuestBookException("Unable to connect to database.", ex);
  55.         }
  56.     }
  57.  
  58.     public User getUser(String login) {
  59.         try {
  60.  
  61.             String sql = "select * from user where name = ? ";
  62.  
  63.             PreparedStatement prep = this.connection.prepareStatement(sql);
  64.             prep.setString(1, login);
  65.  
  66.             ResultSet rs = prep.executeQuery();
  67.  
  68.             rs.first();
  69.             /*
  70.             if (!rs.isBeforeFirst()) {
  71.                 System.out.println("No data");
  72.                 return null;
  73.             }
  74. */
  75.             User user = new User(rs.getInt("iduser"), rs.getString("name"), rs.getString("password"), rs.getString("email"));
  76.             rs.close();
  77.             prep.close();
  78.             return user;
  79.         } catch (SQLException ex) {
  80.             throw new GuestBookException("Unable to retrieve user.", ex);
  81.         }
  82.     }
  83.    
  84.     public void addUser(String login, String password){
  85.        
  86.         try {
  87.             String sql = "insert into user(name, password) values(?, ?)";
  88.            
  89.             PreparedStatement prep = this.connection.prepareStatement(sql);
  90.             prep.setString(1 , login);
  91.             prep.setString(2 , password);
  92.            
  93.             prep.executeUpdate();
  94.             prep.close();
  95.         } catch (SQLException ex) {
  96.             throw new GuestBookException("Unable to add new user.", ex);
  97.         }
  98.     }
  99.  
  100.     public void addMessage(Message message) {
  101.         try {
  102.             String sql = "insert into message(senderID, text, date) values(?, ?, ?)";
  103.  
  104.             PreparedStatement prep = this.connection.prepareStatement(sql);
  105.             prep.setInt(1, message.getUser().getID());
  106.             prep.setString(2, message.getBody());
  107.             prep.setDate(3, (Date) message.getDate());
  108.  
  109.             prep.executeUpdate();
  110.  
  111.             prep.close();
  112.         } catch (SQLException ex) {
  113.             throw new GuestBookException("Unable to add movie.", ex);
  114.         }
  115.     }
  116.    
  117.     public List<LoginAttempt> getLoginAttempts()
  118.     {
  119.         try
  120.         {
  121.             List<LoginAttempt> genres = new ArrayList<>();
  122.  
  123.             String sql = "select * from logginattempt";
  124.  
  125.             PreparedStatement prep = this.connection.prepareStatement(sql);
  126.  
  127.             ResultSet rs = prep.executeQuery();
  128.  
  129.             while (rs.next())
  130.             {
  131.                 //LoginAttempt g = new LoginAttempt(rs.getInt("id"), rs.getString("name"));
  132.                 //genres.add(g);
  133.             }
  134.            
  135.             rs.close();
  136.             prep.close();
  137.  
  138.             return genres;
  139.         }
  140.         catch (SQLException ex)
  141.         {
  142.             throw new GuestBookException("Unable to retrieve login attempts.", ex);
  143.         }
  144.     }
  145.  
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top