jdbc환경설정(mysql)이란?


JDBC 실행

실행전 환경설정(드라이버설치,라이브러리설치,Bean설정)


pom.xml(mysql과Spring-jdbc추가)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- MySQL database driver -->
 
<dependency>
 
    <groupId>mysql</groupId>
 
    <artifactId>mysql-connector-java</artifactId>
 
    <version>5.1.9</version>
 
</dependency>
 
 
<dependency>
 
    <groupId>org.springframework</groupId>
 
    <artifactId>spring-jdbc</artifactId>
 
    <version>4.3.12.RELEASE</version>
 
</dependency>
</beans>




프로퍼티파일에 추가하기.

1
2
3
4
5
6
7
db.driverClass=com.mysql.jdbc.Driver
 
db.url=jdbc:mysql://주소명:포트명/db명?characterEncoding=UTF-8
 
db.uesrname=아이디
 
db.password=비밀번호


bean.xml에 추가하기.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- componnent scan설정 (패키지)-->
 
<context:component-scan base-package="myspring.user" />
 
 
 
 
 
<!-- dataSource등록 -->
 
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 
    <property name="driverClass" value="${db.driverClass}" />
 
    <property name="url" value="${db.url}" />
 
    <property name="username" value="${db.uesrname}" />
 
    <property name="password" value="${db.password}" />
 
</bean>


Test 클래스 코드작성.

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
@RunWith(SpringJUnit4ClassRunner.class)
 
@ContextConfiguration(locations="classpath:config/beans.xml")
 
public class HelloUserClient {
 
    @Autowired
 
    ApplicationContext context;
 
     
 
    @Test
 
    public void dataSourceTest() {
 
        DataSource ds = (DataSource) context.getBean("dataSource");
 
        try {
 
            System.out.println(ds.getConnection());
 
        } catch (SQLException e) {
 
            e.printStackTrace();
 
        }
 
    }
 
}


연결된것 확인방법.

콘솔로그에 com.mysql.jdbc.JDBC4Connection@31368b99 출력되는 것 확인한다.

+ Recent posts