-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDAO.java
More file actions
58 lines (50 loc) · 1.84 KB
/
Copy pathUserDAO.java
File metadata and controls
58 lines (50 loc) · 1.84 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
package User_DAOHandler;
import DB_Util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDAO {
// Insert User
public int insertUser(String username, String uEmail, Long phoneno) {
String sql = "INSERT INTO users (username, uEmail, phoneno) VALUES (?, ?, ?)";//?are called as place holders
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, username);
pstmt.setString(2, uEmail);
pstmt.setLong(3, phoneno);
return pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
e.getMessage();
}
return 0;
}
// Delete User
public int deleteUser(int userID) {
String sql = "DELETE FROM Users WHERE userID = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userID);
return pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
// Update User
public int updateUser( int userID,String username, String uEmail, Long phoneno) {
String sql = "UPDATE users SET username=?, uEmail=?, phoneno=? WHERE userID=?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, username);
pstmt.setString(2, uEmail);
pstmt.setLong(3, phoneno);
pstmt.setInt(4, userID);
return pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}