码迷,mamicode.com
首页 > 数据库 > 详细

Spring的JDBC(非web程序)的简单例子

时间:2015-07-18 19:58:45      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

第一步:

spring配置applicationContext.xml文件,放在src下面:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">

 

      //  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

     // <property name="url" value="jdbc:oracle:thin:@localhost:1521:db10g"/>

    <property name="url" value="jdbc:mysql://localhost:3306/iminer"/>

        <property name="username" value="test"/>

        <property name="password" value="pwd"/>

    </bean>

    <bean id="jdbcTemplate"

       class="org.springframework.jdbc.core.JdbcTemplate">

       <property name="dataSource">

           <ref bean="dataSource" />

       </property>

    </bean>

    <bean id="jdbcUtil" class="com.maggie.util.JdbcUtil">

       <property name="jdbcTemplate">

           <ref bean="jdbcTemplate" />

       </property>

    </bean>

</beans>

 

----------------------------------------------------------------------------------------

第二步:com.maggie.util.JdbcUtil.java 类文件:

 

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">

 

      //  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

     // <property name="url" value="jdbc:oracle:thin:@localhost:1521:db10g"/>

    <property name="url" value="jdbc:mysql://localhost:3306/iminer"/>

        <property name="username" value="test"/>

        <property name="password" value="pwd"/>

    </bean>

    <bean id="jdbcTemplate"

       class="org.springframework.jdbc.core.JdbcTemplate">

       <property name="dataSource">

           <ref bean="dataSource" />

       </property>

    </bean>

    <bean id="jdbcUtil" class="com.maggie.util.JdbcUtil">

       <property name="jdbcTemplate">

           <ref bean="jdbcTemplate" />

       </property>

    </bean>

</beans>

 

 

 

----------------------------------------------------------------------------------------

第三步:测试类:

package com.maggie.test;

 

import java.util.Map;

 

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

 

import com.maggie.util.JdbcUtil;

 

public class Test{

 

    private JdbcUtil jdbc;

 

    public Test() {

       ClassPathResource res = new ClassPathResource("applicationContext.xml");

        XmlBeanFactory factory = new XmlBeanFactory(res);

        jdbc = (JdbcUtil) factory.getBean("jdbcUtil");

    }

   

    public String getUsernameById(String id) {

       Map dataName = jdbc.getJdbcTemplate().queryForMap(

              "select t.username from tablename t where t.id=?", new Object[] { id });

       return (String) dataName.get("username");

    }

   

    public static void main(String[] args) {

       Test test = new Test();

       String id = "1";

       String username = test.getUsernameById(id);

       System.out.println("*************** username = "+username);

 

    }

 

}

 

Spring的JDBC(非web程序)的简单例子

标签:

原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4657427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!