package model; import db.DBConnection; import java.sql.*; public class User implements Auth { private Connection con; private PreparedStatement ps = null; ResultSet rs = null; private int userId; @Override public boolean login(String username, String password) { String query = "select * from user where username=? and password=?"; try { this.con = DBConnection.getConnection(); ps = con.prepareStatement(query); ps.setString(1, username); ps.setString(2, password); ps.execute(); rs = ps.getResultSet(); if(rs.next()){ this.userId = rs.getInt("user_id"); return true; } } catch (SQLException ex){ System.out.println(ex); } return false; } @Override public void register(String username, String password, String name, String address) throws SQLException { String query = "insert into user (username, password, name, address) values(?,?,?,?)"; try { this.con = DBConnection.getConnection(); ps = con.prepareStatement(query); ps.setString(1, username); ps.setString(2, password); ps.setString(3, name); ps.setString(4, address); ps.execute(); } catch (SQLException ex) { System.out.println(ex); } } @Override public void delete(int userId) throws SQLException { String query = "delete from user where user_id = ?"; try { this.con = DBConnection.getConnection(); ps = con.prepareStatement(query); ps.setInt(1, userId); ps.execute(); } catch (SQLException ex) { System.out.println(ex); } } @Override public void update(int userId, String nPassword) throws SQLException { String query = "update user set password=? where user_id=?"; try { this.con = DBConnection.getConnection(); ps = con.prepareStatement(query); ps.setString(1, nPassword); ps.setInt(2, userId); } catch (SQLException ex) { System.out.println(ex); } } public int getUserId() { return this.userId; } }