forked from SpooderManEXE/Useful-College-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAO.java
More file actions
125 lines (104 loc) · 3.59 KB
/
DAO.java
File metadata and controls
125 lines (104 loc) · 3.59 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package net.project.feemanagement.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import net.project.feemanagemet.model.User;
public class UserDAO {
private String jdbcURL= "jdbc:mysql://localhost:3306/student";
private String jdbcUsername = "root";
private String jdbcPassword = "password";
private static final String INSERT_USERS_SQL = "INSERT INTO users "+"(name, amount, state) VALUES" + "(?, ?, ?);";
private static final String SELECT_USER_BY_ID="select id, name, amount , state from users where id = ?";
private static final String SELECT_ALL_USERS="select * from users";
private static final String DELETE_USERS_SQL="delete from users where id = ?;";
private static final String UPDATE_USERS_SQL="update users set name = ?, amount = ?, state = ?, where id = ?;";
protected Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public void insertUser(User user) throws SQLException {
try(Connection connection = getConnection();
PreparedStatement pst = connection.prepareStatement(INSERT_USERS_SQL)){
pst.setString(1,user.getName());
pst.setString(2,user.getAmount());
pst.setString(3,user.getState());
pst.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}
}
public boolean updateUser(User user) throws SQLException {
boolean rowUpdated;
try(Connection connection = getConnection();
PreparedStatement pst = connection.prepareStatement(UPDATE_USERS_SQL)){
pst.setString(1,user.getName());
pst.setString(2,user.getAmount());
pst.setString(3,user.getState());
pst.executeUpdate();
pst.setInt(4,user.getId());
rowUpdated = pst.executeUpdate() > 0;
}
return rowUpdated;
}
public User selectUser(int id) throws SQLException {
User user= null;
try(Connection connection = getConnection();
PreparedStatement pst= connection.prepareStatement(SELECT_USER_BY_ID);){
pst.setInt(1, id);
System.out.println(pst);
ResultSet rs= pst.executeQuery();
while(rs.next()) {
String name = rs.getString("name");
String amount= rs.getString("amount");
String state = rs.getString("State");
user = new User(id, name, amount, state);
}
}
catch(SQLException e) {
e.printStackTrace();
}
return user;
}
public List<User> selectAllUser() throws SQLException {
List<User> users = new ArrayList<>();
try(Connection connection = getConnection();
PreparedStatement pst= connection.prepareStatement(SELECT_ALL_USERS);){
System.out.println(pst);
ResultSet rs= pst.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String amount = rs.getString("amount");
String state = rs.getString("State");
users.add(new User(id, name, amount, state));
}
}
catch(SQLException e) {
e.printStackTrace();
}
return users;
}
public boolean deleteUser(int id) throws SQLException{
boolean rowDeleted;
try(Connection connection = getConnection();
PreparedStatement pst= connection.prepareStatement(DELETE_USERS_SQL);){
pst.setInt(1, id);
rowDeleted = pst.executeUpdate() > 0;
}
return rowDeleted;
}
}