-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminDao.java
More file actions
55 lines (46 loc) · 1.94 KB
/
Copy pathAdminDao.java
File metadata and controls
55 lines (46 loc) · 1.94 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
package com.dao;
import com.entity.Admin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AdminDao {
private static final String INSERT_SQL = "INSERT INTO admin (username, email, password) VALUES (?, ?, ?)";
private static final String SELECT_BY_USERNAME_SQL = "SELECT * FROM admin WHERE username = ?";
public boolean insertPatient(Connection connection, String username, String email, String password) {
try (PreparedStatement preparedStatement = connection.prepareStatement(INSERT_SQL)) {
preparedStatement.setString(1, username);
preparedStatement.setString(2, email);
preparedStatement.setString(3, password);
int rowsAffected = preparedStatement.executeUpdate();
return rowsAffected > 0;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Admin getAdminByUsername(Connection connection, String username) throws SQLException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Admin admin = null;
try {
preparedStatement = connection.prepareStatement(SELECT_BY_USERNAME_SQL);
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
admin = new Admin();
admin.setUsername(resultSet.getString("username"));
admin.setPassword(resultSet.getString("password"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
}
return admin;
}
}