Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. package rest.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import rest.model.User;
  12. import rest.util.DbUtil;
  13.  
  14. public class UserDAO {
  15.  
  16.     private static Connection connection = DbUtil.getConnection();
  17.    
  18.     public static User addUser(User user) {
  19.         try {
  20.             PreparedStatement pStmt = connection
  21.                     .prepareStatement("insert into users(username, password) values (?, ?)", Statement.RETURN_GENERATED_KEYS);
  22.             pStmt.setString(1, user.getUsername());
  23.             pStmt.setString(2, user.getPassword());
  24.             pStmt.executeUpdate();
  25.             ResultSet rs = pStmt.getGeneratedKeys();
  26.             if (rs.next()) {
  27.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  28.             }          
  29.         } catch (SQLException e) {
  30.             e.printStackTrace();
  31.         }
  32.  
  33.         return null;
  34.     }
  35.  
  36.     public static User updateUser(int id, User user) {
  37.         try {
  38.             PreparedStatement pStmt = connection.prepareStatement("update users set username=?, password=? where id=?", Statement.RETURN_GENERATED_KEYS);
  39.             pStmt.setString(1, user.getUsername());
  40.             pStmt.setString(2, user.getPassword());
  41.             pStmt.setInt(3, id);
  42.             pStmt.executeUpdate();
  43.             ResultSet rs = pStmt.getGeneratedKeys();
  44.             if (rs.next()) {
  45.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  46.             }          
  47.         } catch (SQLException e) {
  48.             e.printStackTrace();
  49.         }
  50.  
  51.         return null;
  52.     }
  53.  
  54.     public static void deleteUser(int id) {
  55.         try {
  56.             PreparedStatement pStmt = connection.prepareStatement("delete from users where id=?");
  57.             pStmt.setInt(1, id);
  58.             pStmt.executeUpdate();
  59.         } catch (SQLException e) {
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.  
  64.     public static List<User> getAllUsers() {
  65.         List<User> users = new ArrayList<User>();
  66.         try {
  67.             Statement stmt = connection.createStatement();
  68.             ResultSet rs = stmt.executeQuery("select * from users");
  69.             while (rs.next()) {
  70.                 User user = new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  71.                 users.add(user);
  72.             }
  73.         } catch (SQLException e) {
  74.             e.printStackTrace();
  75.         }
  76.  
  77.         return users;
  78.     }
  79.  
  80.     public static User getUser(int id) {
  81.         try {
  82.             PreparedStatement pStmt = connection.prepareStatement("select * from users where id=?");
  83.             pStmt.setInt(1, id);
  84.             ResultSet rs = pStmt.executeQuery();
  85.             if (rs.next()) {
  86.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  87.             }
  88.         } catch (SQLException e) {
  89.             e.printStackTrace();
  90.         }
  91.  
  92.         return null;
  93.     }
  94.  
  95.     public static User getUserByUsername(String username) {
  96.         try {
  97.             PreparedStatement pStmt = connection.prepareStatement("select * from users where username=?");
  98.             pStmt.setString(1, username);
  99.             ResultSet rs = pStmt.executeQuery();
  100.             if (rs.next()) {
  101.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  102.             }
  103.         } catch (SQLException e) {
  104.             e.printStackTrace();
  105.         }
  106.  
  107.         return null;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement