夢幻之旅

          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 閱讀(594) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 乐平市| 阿拉善右旗| 丰城市| 千阳县| 措美县| 闽侯县| 墨玉县| 南通市| 宁海县| 平邑县| 广河县| 宝山区| 南京市| 嘉荫县| 涡阳县| 正定县| 河东区| 黔西| 五台县| 彩票| 习水县| 玉环县| 黄大仙区| 克什克腾旗| 晋城| 富蕴县| 江陵县| 吴桥县| 三亚市| 闵行区| 韶山市| 澄城县| 怀仁县| 长宁区| 诸城市| 来宾市| 长顺县| 丹巴县| 雅安市| 庆城县| 杂多县|