夢幻之旅

          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 閱讀(602) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 宜都市| 广西| 灯塔市| 茌平县| 湘潭市| 托里县| 宜城市| 兴仁县| 巴塘县| 河间市| 新昌县| 平湖市| 衡东县| 双桥区| 梁河县| 乃东县| 岳阳县| 文昌市| 天台县| 游戏| 格尔木市| 休宁县| 介休市| 江安县| 柘城县| 烟台市| 梁河县| 齐河县| 工布江达县| 广东省| 墨江| 夏津县| 麻江县| 阿拉尔市| 昔阳县| 方正县| 绥棱县| 都昌县| 井冈山市| 武城县| 鸡东县|