-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDao.java
More file actions
107 lines (83 loc) · 2.44 KB
/
UserDao.java
File metadata and controls
107 lines (83 loc) · 2.44 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
package technical.blog.com.dao;
import java.sql.*;
import technical.blog.com.entities.User;
public class UserDao {
private Connection con;
public UserDao(Connection con) {
this.con = con;
}
// save user to database
public boolean saveUser(User user)
{
boolean f=false;
try {
String query="insert into user(name,email,password,city,gender,about) values(?,?,?,?,?,?)";
PreparedStatement psmt=con.prepareStatement(query);
psmt.setString(1, user.getName());
psmt.setString(2, user.getEmail());
psmt.setString(3, user.getPassword());
psmt.setString(4, user.getCity());
psmt.setString(5,user.getGender());
psmt.setString(6,user.getAbout());
psmt.executeUpdate();
f=true;
}catch(Exception e)
{
e.printStackTrace();
}
return f;
}
// get user by user email id and user password from db
public User getUserByEmailAndPassword(String email,String password)
{
User user=null;
try {
String query="select * from user where email=? and password=?";
PreparedStatement psmt=con.prepareStatement(query);
psmt.setString(1, email);
psmt.setString(2, password);
ResultSet rs=psmt.executeQuery();
while(rs.next())
{
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
user.setPassword(rs.getString("password"));
user.setCity(rs.getString("city"));
user.setGender(rs.getString("gender"));
user.setAbout(rs.getString("about"));
user.setRdate(rs.getTimestamp("rdate"));
user.setProfile(rs.getString("profile"));
}
}
catch(Exception e)
{
e.printStackTrace();
}
return user;
}
// update user detail in db
public boolean updateUser(User user)
{
boolean f=false;
try {
String query="update user set name=?,email=?,password=?,city=?,gender=?,about=?,profile=? where id=?";
PreparedStatement psmt=con.prepareStatement(query);
psmt.setString(1, user.getName());
psmt.setString(2, user.getEmail());
psmt.setString(3, user.getPassword());
psmt.setString(4, user.getCity());
psmt.setString(5,user.getGender());
psmt.setString(6,user.getAbout());
psmt.setString(7, user.getProfile());
psmt.setInt(8, user.getId());
psmt.executeUpdate();
f=true;
}catch(Exception e)
{
e.printStackTrace();
}
return f;
}
}