#21 jdbc: refactoring
This commit is contained in:
@@ -3,15 +3,40 @@ package org.example;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ConnectionManager {
|
||||
public static DataSource getDataSource() {
|
||||
HikariDataSource hikariDataSource = new HikariDataSource();
|
||||
hikariDataSource.setDriverClassName("org.h2.Driver");
|
||||
hikariDataSource.setJdbcUrl("jdbc:h2:mem://localhost/~/jdbc-practice;MODE=MySQL;DB_CLOSE_DELAY=-1");
|
||||
hikariDataSource.setUsername("sa");
|
||||
hikariDataSource.setPassword("");
|
||||
|
||||
return hikariDataSource;
|
||||
public static final String DB_DRIVER = "org.h2.Driver";
|
||||
public static final String DB_URL = "jdbc:h2:mem://localhost/~/jdbc-practice;MODE=MySQL;DB_CLOSE_DELAY=-1";
|
||||
public static final String DB_USERNAME = "sa";
|
||||
public static final String DB_PASSWORD = "";
|
||||
public static final int DB_MAX_POOL_SIZE = 40;
|
||||
|
||||
private static final DataSource ds;
|
||||
|
||||
static {
|
||||
HikariDataSource hikariDataSource = new HikariDataSource();
|
||||
hikariDataSource.setDriverClassName(DB_DRIVER);
|
||||
hikariDataSource.setJdbcUrl(DB_URL);
|
||||
hikariDataSource.setUsername(DB_USERNAME);
|
||||
hikariDataSource.setPassword(DB_PASSWORD);
|
||||
hikariDataSource.setMaximumPoolSize(DB_MAX_POOL_SIZE);
|
||||
hikariDataSource.setMinimumIdle(DB_MAX_POOL_SIZE);
|
||||
|
||||
ds = hikariDataSource;
|
||||
}
|
||||
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
return ds.getConnection();
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static DataSource getDataSource() {
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.example;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class JdbcTemplate {
|
||||
|
||||
public void executeUpdate(User user, String sql, PreparedStatementSetter pss) throws SQLException {
|
||||
Connection con = null;
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
try {
|
||||
con = ConnectionManager.getConnection();
|
||||
|
||||
pstmt = con.prepareStatement(sql);
|
||||
pss.setter(pstmt);
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} finally {
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object executeQuery(String sql, PreparedStatementSetter pss, RowMapper rowMapper) throws SQLException {
|
||||
Connection con = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
con = ConnectionManager.getConnection();
|
||||
pstmt = con.prepareStatement(sql);
|
||||
pss.setter(pstmt);
|
||||
|
||||
rs = pstmt.executeQuery();
|
||||
|
||||
Object obj = null;
|
||||
|
||||
if (rs.next()) {
|
||||
obj = rowMapper.map(rs);
|
||||
}
|
||||
|
||||
return obj;
|
||||
} finally {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
}
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.example;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public interface PreparedStatementSetter {
|
||||
|
||||
void setter(PreparedStatement pstmt) throws SQLException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.example;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public interface RowMapper {
|
||||
|
||||
Object map(ResultSet rs) throws SQLException;
|
||||
}
|
||||
@@ -4,80 +4,31 @@ import java.sql.*;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
private Connection getConnection() {
|
||||
String url = "jdbc:h2:mem://localhost/~/jdbc-practice;MODE=MySQL;DB_CLOSE_DELAY=-1";
|
||||
String id = "sa";
|
||||
String pw = "";
|
||||
|
||||
try {
|
||||
Class.forName("org.h2.Driver");
|
||||
return DriverManager.getConnection(url, id, pw);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void create(User user) throws SQLException {
|
||||
Connection con = null;
|
||||
PreparedStatement pstmt = null;
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
|
||||
try {
|
||||
con = getConnection();
|
||||
String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)";
|
||||
String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)";
|
||||
|
||||
pstmt = con.prepareStatement(sql);
|
||||
jdbcTemplate.executeUpdate(user, sql, pstmt -> {
|
||||
pstmt.setString(1, user.getUserId());
|
||||
pstmt.setString(2, user.getPassword());
|
||||
pstmt.setString(3, user.getName());
|
||||
pstmt.setString(4, user.getEmail());
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} finally {
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public User findByUserId(String userId) throws SQLException {
|
||||
Connection con = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
String sql = "SELECT userId, password, name, email FROM USERS WHERE userId = ?";
|
||||
|
||||
try {
|
||||
con = getConnection();
|
||||
String sql = "SELECT userId, password, name, email FROM USERS WHERE userId = ?";
|
||||
|
||||
pstmt = con.prepareStatement(sql);
|
||||
pstmt.setString(1, userId);
|
||||
|
||||
rs = pstmt.executeQuery();
|
||||
|
||||
User user = null;
|
||||
|
||||
if (rs.next()) {
|
||||
user = new User(
|
||||
return (User) jdbcTemplate.executeQuery(
|
||||
sql,
|
||||
(pstmt) -> pstmt.setString(1, userId),
|
||||
(rs) -> new User(
|
||||
rs.getString("userId"),
|
||||
rs.getString("password"),
|
||||
rs.getString("name"),
|
||||
rs.getString("email")
|
||||
);
|
||||
}
|
||||
|
||||
return user;
|
||||
} finally {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
}
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user