-
Notifications
You must be signed in to change notification settings - Fork 4
/
UserDAOImpl.java
94 lines (80 loc) · 2.35 KB
/
UserDAOImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.beans.User;
import com.connections.DatabaseConnection;
import com.daos.UserDAO;
public class UserDAOImpl implements UserDAO {
@Override
public int addUser(User user) {
int rowsAdded=0;
String ADDUSER="INSERT INTO USERS VALUES(?,?)";
Connection con=DatabaseConnection.openConnection();
try {
PreparedStatement ps=con.prepareStatement(ADDUSER);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPasscode());
rowsAdded=ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
// TODO Handle exceptions properly
e.printStackTrace();
}
return rowsAdded;
}
@Override
public User findUserbyUsername(String Username) {
User user=null;
String FIND_BY_USERNAME="SELECT * FROM USERS WHERE USERNAME=?";
try(Connection con=DatabaseConnection.openConnection();)
{
PreparedStatement ps=con.prepareStatement(FIND_BY_USERNAME);
ps.setString(1, Username);
ResultSet set=ps.executeQuery();
if(set.next())
{
String Passcode = set.getString("passcode");
user=new User(null, Username, Passcode);
}
} catch(SQLException e)
{
e.printStackTrace();
}
return user;
}
@Override
public int deleteUser(String Username) {
int rowsModified=0;
String REMOVE_USER="DELETE FROM USERS WHERE USERNAME=?";
try(Connection con=DatabaseConnection.openConnection();)
{
PreparedStatement ps=con.prepareStatement(REMOVE_USER);
ps.setString(1, Username);
rowsModified = ps.executeUpdate();
} catch(SQLException e)
{
e.printStackTrace();
}
return rowsModified;
}
@Override
public int modifyUser(User user) {
// TODO Auto-generated method stub
int rowsModified=0;
String MODIFY_USER="UPDATE USERS SET USERS.PASSCODE=?, USERS.USERNAME=? WHERE USERNAME=?";
try(Connection con=DatabaseConnection.openConnection();)
{
PreparedStatement ps=con.prepareStatement(MODIFY_USER);
ps.setString(3, user.getUsername());
ps.setString(1, user.getPasscode());
ps.setString(2, user.getUsername());
rowsModified = ps.executeUpdate();
} catch(SQLException e)
{
e.printStackTrace();
}
return rowsModified;
}
}