Advertisement
Guest User

Untitled

a guest
May 7th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. package rest.dao;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Properties;
  16.  
  17. import rest.model.User;
  18. import rest.util.DbUtil;
  19.  
  20. public class UserDAO {
  21.  
  22.     private static Connection connection = DbUtil.getConnection();
  23.  
  24.     public static User addUser(String username, String password, InputStream input) {
  25.         try {
  26.             PreparedStatement pStmt = connection.prepareStatement("insert into users(username, password) values (?, ?)",
  27.                     Statement.RETURN_GENERATED_KEYS);
  28.             pStmt.setString(1, username);
  29.             pStmt.setString(2, password);
  30.             pStmt.executeUpdate();
  31.             ResultSet rs = pStmt.getGeneratedKeys();
  32.             if (rs.next()) {
  33.                 uploadFile(input, rs.getInt("id"));
  34.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  35.             }
  36.         } catch (SQLException e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         return null;
  41.     }
  42.  
  43.     public static User updateUser(int id, String username, String password, InputStream input) {
  44.         try {
  45.             PreparedStatement pStmt = connection.prepareStatement("update users set username=?, password=? where id=?",
  46.                     Statement.RETURN_GENERATED_KEYS);
  47.             pStmt.setString(1, username);
  48.             pStmt.setString(2, password);
  49.             pStmt.setInt(3, id);
  50.             pStmt.executeUpdate();
  51.             ResultSet rs = pStmt.getGeneratedKeys();
  52.             if (rs.next()) {
  53.                 if(input != null)
  54.                     uploadFile(input, rs.getInt("id"));
  55.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  56.             }
  57.         } catch (SQLException e) {
  58.             e.printStackTrace();
  59.         }
  60.  
  61.         return null;
  62.     }
  63.  
  64.     public static void deleteUser(int id) {
  65.         try {
  66.             PreparedStatement pStmt = connection.prepareStatement("delete from users where id=?");
  67.             pStmt.setInt(1, id);
  68.             pStmt.executeUpdate();
  69.         } catch (SQLException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.  
  74.     public static List<User> getAllUsers() {
  75.         List<User> users = new ArrayList<User>();
  76.         try {
  77.             Statement stmt = connection.createStatement();
  78.             ResultSet rs = stmt.executeQuery("select * from users order by id");
  79.             while (rs.next()) {
  80.                 User user = new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  81.                 users.add(user);
  82.             }
  83.         } catch (SQLException e) {
  84.             e.printStackTrace();
  85.         }
  86.  
  87.         return users;
  88.     }
  89.  
  90.     public static User getUser(int id) {
  91.         try {
  92.             PreparedStatement pStmt = connection.prepareStatement("select * from users where id=?");
  93.             pStmt.setInt(1, id);
  94.             ResultSet rs = pStmt.executeQuery();
  95.             if (rs.next()) {
  96.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  97.             }
  98.         } catch (SQLException e) {
  99.             e.printStackTrace();
  100.         }
  101.  
  102.         return null;
  103.     }
  104.  
  105.     public static User getUserByUsername(String username) {
  106.         try {
  107.             PreparedStatement pStmt = connection.prepareStatement("select * from users where username=?");
  108.             pStmt.setString(1, username);
  109.             ResultSet rs = pStmt.executeQuery();
  110.             if (rs.next()) {
  111.                 return new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"));
  112.             }
  113.         } catch (SQLException e) {
  114.             e.printStackTrace();
  115.         }
  116.  
  117.         return null;
  118.     }
  119.  
  120.     private static void uploadFile(InputStream uploadedInputStream, int id) {
  121.         try {
  122.             InputStream inputStream = DbUtil.class.getClassLoader().getResourceAsStream("uploads.properties");
  123.             Properties prop = new Properties();
  124.             prop.load(inputStream);
  125.             String folder = prop.getProperty("folder");
  126.             String filePath = folder + id;
  127.             saveFile(uploadedInputStream, filePath);
  128.         } catch (Exception e) {
  129.             e.printStackTrace();
  130.         }
  131.     }
  132.  
  133.     private static void saveFile(InputStream uploadedInputStream, String serverLocation) {
  134.  
  135.         try {
  136.             OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
  137.             int read = 0;
  138.             byte[] bytes = new byte[1024];
  139.  
  140.             outpuStream = new FileOutputStream(new File(serverLocation));
  141.             while ((read = uploadedInputStream.read(bytes)) != -1) {
  142.                 outpuStream.write(bytes, 0, read);
  143.             }
  144.             outpuStream.flush();
  145.             outpuStream.close();
  146.         } catch (IOException e) {
  147.  
  148.             e.printStackTrace();
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement