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

Commit

Permalink
[ch01] Use DataSource to get connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Jul 29, 2017
1 parent 87cb981 commit 7045fc5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<version>${spring.framework-version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.framework-version}</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/ch01/springbook/user/dao/DaoFactory.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package ch01.springbook.user.dao;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

@Configuration
public class DaoFactory {

@Bean
public UserDao userDao() {
return new UserDao(connectionMaker());
UserDao userDao = new UserDao();
userDao.setDataSource(dataSource());
return userDao;
}

@Bean
public ConnectionMaker connectionMaker() {
return new SimpleConnectionMaker();
}

@Bean
public DataSource dataSource() {
SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();

simpleDriverDataSource.setDriverClass(com.mysql.jdbc.Driver.class);
simpleDriverDataSource.setUrl("jdbc:mysql://localhost/tobystudy?useUnicode=true&characterEncoding=UTF-8&useSSL=true&verifyServerCertificate=false");
simpleDriverDataSource.setUsername("study");
simpleDriverDataSource.setPassword("study");

return simpleDriverDataSource;
}
}
9 changes: 8 additions & 1 deletion src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import ch01.springbook.user.domain.User;

public class UserDao {

private ConnectionMaker connectionMaker;
private DataSource dataSource;

public UserDao() {

Expand All @@ -19,12 +22,16 @@ public UserDao(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public void setConnectionMaker(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

public void add(User user) throws ClassNotFoundException, SQLException {
Connection c = connectionMaker.makeNewConnection();
Connection c = dataSource.getConnection();

PreparedStatement ps = c.prepareStatement(
"INSERT INTO users(id, name, password) VALUES(?, ?, ?)");
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<bean id="connectionMaker" class="ch01.springbook.user.dao.SimpleConnectionMaker" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/tobystudy?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=true&amp;verifyServerCertificate=false" />
<property name="username" value="study" />
<property name="password" value="study" />
</bean>

<bean id="userDao" class="ch01.springbook.user.dao.UserDao">
<property name="connectionMaker" ref="connectionMaker" />
<property name="dataSource" ref="dataSource" />
</bean>

</beans>

0 comments on commit 7045fc5

Please sign in to comment.