夢幻之旅

          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
          主站蜘蛛池模板: 皮山县| 淅川县| 赤峰市| 东丽区| 招远市| 交城县| 通海县| 德清县| 新安县| 马公市| 铜山县| 即墨市| 郴州市| 沙洋县| 新兴县| 墨脱县| 汽车| 和硕县| 平利县| 旬阳县| 大渡口区| 墨脱县| 南城县| 阿拉善左旗| 响水县| 同江市| 黄龙县| 茌平县| 兰西县| 汝城县| 陵水| 台山市| 凤山市| 桂东县| 西和县| 普陀区| 上饶县| 古田县| 陆川县| 峨眉山市| 阜新市|