반응형
1. 필요한 lib
commons-logging.jar
ojdbc14.jar
postgresql.jdbc3.jar
spring.jar
tomcat-dbcp.jar
2. 테스트 소스
(1) DataStuff.java (JdbcTemplate를 이용해서 처리)
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class DataStuff {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
List getNames() {
return jdbcTemplate.queryForList("select * from youn"); //youn은 테이블 이름
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
(2) 설정파일 - dataAccessContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:context=http://www.springframework.org/schema/context
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/constex/spring-context-2.5.xsd">
<bean id="dataStuff" class="DataStuff">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource”
p:driverClassName="org.postgresql.Driver"
p:url="jdbc:postgresql://192.168.10.241:5432/manager"
p:username="test"
p:password="test"/>
</beans>
(3) 실행파일 - JustStuff.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml,XmlBeanFactory;
import org.springframework.core.io.*;
public class JustStuff
{
public static void main(String args[])
{
Resource resource = new ClassPathResource("dataAccessContext.xml"); //설정 파일 이름
BeanFactory beanFactory = new XmlBeanFactory(resource);
DataStuff test = (DataStuff)beanFactory.getBean("dataStuff");
System.out.println(test.getNames());
System.out.println("OK!");
}
}
728x90
반응형
'웹 개발' 카테고리의 다른 글
JSP, HttpURLConnection, Servlet 한글 출력방법 (0) | 2019.08.11 |
---|---|
JSP 페이지 이동 4가지 방법 및 특성 (0) | 2019.08.11 |
[spring] 생성자 혼동 피하기 (0) | 2019.08.11 |
[spring] 같은 팩토리 내부의 빈 주입하기 (0) | 2019.08.11 |
Spring의 특장점 (0) | 2019.08.11 |