梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks

 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation
="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
<property name="driverClass" value="com.mysql.jdbc.Driver" />
        
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hwpok" />
        
<property name="user" value="mydata" />
        
<property name="password" value="123456" />
        
<property name="autoCommitOnClose" value="true"/>
        
<property name="checkoutTimeout" value="5000"/>
        
<property name="initialPoolSize" value="2"/>
        
<property name="minPoolSize" value="2"/>
        
<property name="maxPoolSize" value="4"/>
        
<property name="maxIdleTime" value="25200"/>
        
<property name="acquireIncrement" value="1800"/>
        
<property name="maxIdleTimeExcessConnections" value="5"/>
    
</bean>
    
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     
<property name="dataSource" ref="dataSource" />
    
</bean>
</beans>

测试:
package hvp.spring.jdbc.jdbctemp;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class CreateTable
{
    JdbcTemplate jt;
    
    
public CreateTable()
    
{
        
this.jt = this.getJdbcTemplate2();
    }

    
    
public JdbcTemplate getJdbcTemplate()
    
{
        DriverManagerDataSource dmds 
= new DriverManagerDataSource();
        dmds.setDriverClassName(
"com.mysql.jdbc.Driver");
        dmds.setUrl(
"jdbc:mysql://localhost:3306/hwpok");
        dmds.setUsername(
"hwpok");
        dmds.setPassword(
"123456");
        
        JdbcTemplate jdbcTemplate 
= new JdbcTemplate();
        jdbcTemplate.setDataSource(dmds);
        
return jdbcTemplate;
    }

    
    
public JdbcTemplate getJdbcTemplate2()
    
{
        String configPath 
= "hvp/spring/jdbc/jdbctemp/beans.xml";
        ApplicationContext ctx 
= new ClassPathXmlApplicationContext(configPath);
        
return (JdbcTemplate) ctx.getBean("jdbcTemplate");
    }

    
    
public void createTable()
    
{
        StringBuffer sql 
= new StringBuffer();
        sql.append(
"CREATE TABLE t_user(");
        sql.append(
"user_id int primary key,");
        sql.append(
"user_name varchar(32)");
        sql.append(
")");
        jt.execute(sql.toString());
    }

    
    
public static void main(String[] args)
    
{
        CreateTable ct 
= new CreateTable();
        ct.createTable();
    }

}

posted on 2008-08-10 12:36 HUIKK 阅读(574) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: