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 DAO;
- import entities.User;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- /**
- *
- * @author Petru
- */
- public class UserDAO {
- public boolean login(String email, String password){
- // return username.equalsIgnoreCase("abc") && password.equalsIgnoreCase("123");
- try {
- //1
- Class.forName("com.mysql.jdbc.Driver");
- //2
- java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pcstore","root","");
- //3
- PreparedStatement pr = cn.prepareStatement("SELECT * FROM client WHERE email=? AND password=?");
- pr.setString(1, email);
- pr.setString(2, password);
- //4
- ResultSet rs = pr.executeQuery();
- //5
- if (rs.next()){
- User u = new User(rs.getInt("id_client"), rs.getInt("id_type"), rs.getString("name"), rs.getString("email"), rs.getString("password"), rs.getString("address") );
- return email.equalsIgnoreCase(u.getEmail()) && password.equalsIgnoreCase(u.getPassword());
- }
- } catch (Exception ex) {
- //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
- ex.printStackTrace();
- }
- return false;
- }
- public void addUser(User u) {
- try {
- //1
- Class.forName("com.mysql.jdbc.Driver");
- //2
- java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pcstore","root","");
- //3
- PreparedStatement pr = cn.prepareStatement("INSERT INTO client VALUES (NULL,?,?,?,?,?)");
- pr.setString(1, u.getName());
- pr.setString(2, u.getEmail());
- pr.setString(3, u.getPassword());
- pr.setString(4, u.getAddress());
- pr.setInt(5, 1);
- // pr.setInt(4, p.getState());
- //4
- pr.execute();
- } catch (Exception ex) {
- //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
- ex.printStackTrace();
- }
- }
- public boolean isAdmin(String email) {
- try {
- //1
- Class.forName("com.mysql.jdbc.Driver");
- //2
- java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pcstore","root","");
- //3
- PreparedStatement pr = cn.prepareStatement("SELECT * FROM client WHERE email=? AND id_type=2");
- pr.setString(1, email);
- //4
- ResultSet rs = pr.executeQuery();
- //5
- if (rs.next()){
- return true;
- }
- } catch (Exception ex) {
- //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
- ex.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement