Advertisement
Guest User

Untitled

a guest
May 27th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package dal;
  2.  
  3. import bll.User;
  4. import java.io.IOException;
  5. import java.sql.*;
  6.  
  7.  
  8. /**
  9.  *
  10.  * @author AngelwingedDevil
  11.  */
  12. public class UserDB extends DBManager{
  13.     private static Connection con;
  14.     private static UserDB cinstance;
  15.  
  16.     private UserDB() throws IOException
  17.     {
  18.         super();
  19.     }
  20.  
  21.     public static UserDB getDbInstance() throws IOException
  22.     {
  23.         if(cinstance==null)
  24.         {
  25.             cinstance=new UserDB();
  26.         }
  27.         return cinstance;
  28.     }
  29.  
  30.     protected  PreparedStatement prepare(boolean b, User u) throws SQLException, IOException
  31.     {
  32.         con = getConnection();
  33.         java.sql.PreparedStatement ps;
  34.         if (b)
  35.         {
  36.             ps = con.prepareStatement("UPDATE `mediabooking`.`users` SET `username` = ?,`password`=?,`fname` = ?,`lname` = ?,`email` = ?,`phone1` = ?,`phone2` = ?,`address1` = ?,`address2` = ?,`zip` = ?,`type` = ?,`refid` = ? WHERE `users`.`id` =?");
  37.         }
  38.         else
  39.         {
  40.             ps = con.prepareStatement("INSERT INTO `mediabooking`.`users` (username,password,fname,lname,email,phone1,phone2,address1,address2,zip,type,refid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
  41.         }
  42.  
  43.         return setValues(u,ps,b);
  44.     }
  45.  
  46.     protected  PreparedStatement setValues(User u,PreparedStatement ps,boolean b) throws SQLException
  47.     {
  48.         ps.setString(1, u.getUserName());
  49.         ps.setString(2, u.getPassword());
  50.         ps.setString(3, u.getFname());
  51.         ps.setString(4, u.getLname());
  52.         ps.setString(5, u.getEmail());
  53.         ps.setInt(6, u.getPhone1());
  54.         ps.setInt(7, u.getPhone2());
  55.         ps.setString(8, u.getAddress1());
  56.         ps.setString(9, u.getAddress2());
  57.         ps.setInt(10, u.getZip());
  58.         ps.setInt(11, u.getType());
  59.         ps.setString(12, u.getRefId());
  60.         if (b)
  61.         {
  62.             ps.setInt(13, u.getId());
  63.         }
  64.  
  65.         return ps;
  66.     }
  67.  
  68.     protected  User generate(ResultSet rs) throws SQLException
  69.         {
  70.                 int id = rs.getInt("uid");
  71.                 String username = rs.getString("username");
  72.                 String pass = rs.getString("password");
  73.                 String fname = rs.getString("fname");
  74.                 String lname = rs.getString("lname");
  75.                 String email = rs.getString("email");
  76.                 int phone1 = rs.getInt("phone1");
  77.                 int phone2 = rs.getInt("phone2");
  78.                 String address1 = rs.getString("address1");
  79.                 String address2 = rs.getString("address2");
  80.                 int zip = rs.getInt("zip");
  81.                 int type = rs.getInt("type");
  82.                 String refid = rs.getString("refid");
  83.                 return new User(id, username, pass, email, fname, lname,
  84.                         address1, address2, zip, type, phone1, phone2, refid) {};
  85.         }
  86.  
  87.      public int create(User u) throws SQLException, IOException
  88.      {
  89.          try
  90.          {
  91.          PreparedStatement ps = prepare(false, u);
  92.              System.out.println(ps);
  93.          ps.executeQuery();
  94.              System.out.println(ps);
  95.          ResultSet rs = ps.getGeneratedKeys();
  96.  
  97.             rs.next();
  98.             return rs.getInt(1);
  99.         }
  100.         finally
  101.         {
  102.             con.close();
  103.         }
  104.      }
  105.  
  106.      public User loginByUser(String userName,String password) throws SQLException, NullPointerException, IOException
  107.      {
  108.          try
  109.          {
  110.              con = getConnection();
  111.              PreparedStatement ps = con.prepareStatement("SELECT * FROM `mediabooking`.`users` WHERE username=? AND password=?");
  112.              ps.setString(1,userName);
  113.              ps.setString(2,password);
  114.  
  115.              ResultSet rs = ps.executeQuery();
  116.  
  117.              if(rs.next())
  118.                return generate(rs);
  119.          }
  120.          finally{
  121.              con.close();
  122.          }
  123.          return null;
  124.      }
  125.  
  126.      public User loginByEmail(String email,String password) throws SQLException, NullPointerException, IOException
  127.      {
  128.          try
  129.          {
  130.              con = getConnection();
  131.              PreparedStatement ps = con.prepareStatement("SELECT * FROM `mediabooking`.`users` WHERE email=? AND password=?");
  132.              ps.setString(1,email);
  133.              ps.setString(2,password);
  134.  
  135.              ResultSet rs = ps.executeQuery();
  136.  
  137.              if(rs.next())
  138.                return generate(rs);
  139.          }
  140.          finally{
  141.              con.close();
  142.          }
  143.          return null;
  144.      }
  145.  
  146.     public User getUser(int id) throws SQLException
  147.     {
  148.         con = getConnection();
  149.         PreparedStatement ps = con.prepareStatement("SELECT * FROM mediabooking.users WHERE id=?");
  150.         ps.setInt(1, id);
  151.         ResultSet rs =ps.executeQuery();
  152.  
  153.         return generate(rs);
  154.     }
  155.  
  156.    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement