Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package data;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import model.LoginAttempt;
- import model.Message;
- import model.User;
- import util.GuestBookException;
- /**
- *
- * @author Jasper
- */
- public class GuestBookDA {
- private static final String URL = "jdbc:mysql://localhost:3333/guestbook";
- private static final String UID = "admin";
- private static final String PWD = "admin";
- private Connection connection;
- private static GuestBookDA instance;
- public static GuestBookDA getInstance() {
- if (instance == null) {
- instance = new GuestBookDA();
- }
- return instance;
- }
- private GuestBookDA() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection(URL, UID, PWD);
- } catch (ClassNotFoundException ex) {
- throw new GuestBookException("Unable to load database driver.", ex);
- } catch (SQLException ex) {
- throw new GuestBookException("Unable to connect to database.", ex);
- }
- }
- public User getUser(String login) {
- try {
- String sql = "select * from user where name = ? ";
- PreparedStatement prep = this.connection.prepareStatement(sql);
- prep.setString(1, login);
- ResultSet rs = prep.executeQuery();
- rs.first();
- /*
- if (!rs.isBeforeFirst()) {
- System.out.println("No data");
- return null;
- }
- */
- User user = new User(rs.getInt("iduser"), rs.getString("name"), rs.getString("password"), rs.getString("email"));
- rs.close();
- prep.close();
- return user;
- } catch (SQLException ex) {
- throw new GuestBookException("Unable to retrieve user.", ex);
- }
- }
- public void addUser(String login, String password){
- try {
- String sql = "insert into user(name, password) values(?, ?)";
- PreparedStatement prep = this.connection.prepareStatement(sql);
- prep.setString(1 , login);
- prep.setString(2 , password);
- prep.executeUpdate();
- prep.close();
- } catch (SQLException ex) {
- throw new GuestBookException("Unable to add new user.", ex);
- }
- }
- public void addMessage(Message message) {
- try {
- String sql = "insert into message(senderID, text, date) values(?, ?, ?)";
- PreparedStatement prep = this.connection.prepareStatement(sql);
- prep.setInt(1, message.getUser().getID());
- prep.setString(2, message.getBody());
- prep.setDate(3, (Date) message.getDate());
- prep.executeUpdate();
- prep.close();
- } catch (SQLException ex) {
- throw new GuestBookException("Unable to add movie.", ex);
- }
- }
- public List<LoginAttempt> getLoginAttempts()
- {
- try
- {
- List<LoginAttempt> genres = new ArrayList<>();
- String sql = "select * from logginattempt";
- PreparedStatement prep = this.connection.prepareStatement(sql);
- ResultSet rs = prep.executeQuery();
- while (rs.next())
- {
- //LoginAttempt g = new LoginAttempt(rs.getInt("id"), rs.getString("name"));
- //genres.add(g);
- }
- rs.close();
- prep.close();
- return genres;
- }
- catch (SQLException ex)
- {
- throw new GuestBookException("Unable to retrieve login attempts.", ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement