Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch01] Separated the routine of getting DB connection into getConnect…
Browse files Browse the repository at this point in the history
…ion method.
  • Loading branch information
dhsim86 committed Jun 6, 2017
1 parent 59386df commit 30c1d9b
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
public class UserDao {

public void add(User user) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/tobystudy?useUnicode=true&characterEncoding=UTF-8&useSSL=true&verifyServerCertificate=false",
"study",
"study");
Connection c = getConnection();

PreparedStatement ps = c.prepareStatement(
"INSERT INTO users(id, name, password) VALUES(?, ?, ?)");
Expand All @@ -26,11 +22,7 @@ public void add(User user) throws ClassNotFoundException, SQLException {
}

public User get(String id) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/tobystudy?useUnicode=true&characterEncoding=UTF-8&useSSL=true&verifyServerCertificate=false",
"study",
"study");
Connection c = getConnection();

PreparedStatement ps = c.prepareStatement(
"SELECT * FROM users WHERE id = ?");
Expand All @@ -50,4 +42,13 @@ public User get(String id) throws ClassNotFoundException, SQLException {

return user;
}

private Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/tobystudy?useUnicode=true&characterEncoding=UTF-8&useSSL=true&verifyServerCertificate=false",
"study",
"study");
return c;
}
}

0 comments on commit 30c1d9b

Please sign in to comment.