2013년 11월 13일 수요일

스프링 JDBC 예제, Spring Framework JDBC 예제(Spring Framework교육학원, 스피링 교육학원) Spring Simple JDBC 예제

스프링 JDBC 예제, Spring Framework JDBC 예제(Spring Framework교육학원, 스피링 교육학원)
 
Spring Simple JDBC 예제
1.     EmpDao.java
 
package onj.edu.jdbc;
 
import java.util.List;
 
import javax.sql.DataSource;
 
import org.springframework.jdbc.core.JdbcTemplate;
 
public class EmpDao {
       private DataSource dataSource;
       private JdbcTemplate jdbcTemplate;
 
       public DataSource getDataSource() {
             return dataSource;
       }
 
       public void setDataSource(DataSource dataSource) {
             this.dataSource = dataSource;
             this.jdbcTemplate = new JdbcTemplate(dataSource);
       }
      
       List getNames() {
             String sql = "select * from emp where rownum < 5";
             return jdbcTemplate.queryForList(sql);
       }
}
 
 
2.     JdbcTestClient.java
 
package onj.edu.jdbc;
 
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
 
public class JdbcClient {
       public static void main(String[] args) {
 
             GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
             ctx.load("app-jdbc.xml");
             ctx.refresh();
            
             EmpDao e = (EmpDao)ctx.getBean("EmpDao");
             System.out.println(e.getNames());
             ctx.close();
            
       }
}
 
 
3.     app-Jdbc.xml
 
<!--  ojdbc14.jar, commons-collection.jar, commons-dbcp.jsr, commons-pool.jar 필요함 -->
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
             http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
             http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.2.xsd
             http://www.springframework.org/schema/util            http://www.springframework.org/schema/util/spring-util-3.2.xsd">
 
       <bean id="EmpDao" class="onj.edu.jdbc.EmpDao">
             <property name="dataSource">
                    <ref local="dataSource" />
             </property>
       </bean>
 
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
             <property name="driverClassName">
                    <value>oracle.jdbc.driver.OracleDriver</value>
             </property>
             <property name="url">
                    <value>jdbc:oracle:thin:@localhost:1521:onj</value>
             </property>
             <property name="username">
                    <value>scott</value>
             </property>
             <property name="password">
                    <value>tiger</value>
             </property>
       </bean>
 
</beans>               
 
4.     결과
[{EMPNO=7369, ENAME=SMITH, JOB=CLERK, MGR=7902, HIREDATE=1980-12-17 00:00:00.0, SAL=800, COMM=null, DEPTNO=20}, {EMPNO=7499, ENAME=ALLEN, JOB=SALESMAN, MGR=7698, HIREDATE=1981-02-20 00:00:00.0, SAL=1600, COMM=300, DEPTNO=30}, {EMPNO=7521, ENAME=WARD, JOB=SALESMAN, MGR=7698, HIREDATE=1981-02-22 00:00:00.0, SAL=1250, COMM=500, DEPTNO=30}, {EMPNO=7566, ENAME=JONES, JOB=MANAGER, MGR=7839, HIREDATE=1981-04-02 00:00:00.0, SAL=2975, COMM=null, DEPTNO=20}]
 

댓글 없음:

댓글 쓰기