Ryan's Java world!

          something about Java and opensource!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            51 Posts :: 25 Stories :: 59 Comments :: 0 Trackbacks

          Spring in Action 筆記 (II)

          今天來看看使用JDBC來操作數據: 使用的是Derby(JavaDB)數據庫,關于JavaDB的介紹請點擊這里: http://blog.matrix.org.cn/page/icess?catname=%2FJavaDB 。 下面建立一個DatabaseUtils.java的工具類,來操作數據庫 。該類在上面的連接的文章中有講述。

          package? test.jdbc;

          import? java.io.File;
          import? java.io.IOException;
          import? java.io.InputStream;
          import? java.sql.Connection;
          import? java.sql.DriverManager;
          import? java.sql.PreparedStatement;
          import? java.sql.ResultSet;
          import? java.sql.SQLException;
          import? java.sql.Statement;
          import? java.util.Properties;
          import? java.util.logging.Logger;

          public?class? DatabaseUtils?{
          ?? private?static?final? String?DB_PROPERTIES_FILE?=? "jdbc.properties" ;

          ?? private?static?final? String?DB_OPPOSITE_LOCATION?=? "/.test" ;

          ?? static? Logger?logger?=?Logger.getLogger(DatabaseUtils. class .getName());

          ?? private? Connection?dbConnection;

          ?? private? Properties?dbProperties;

          ?? private?boolean? isConnected;

          ?? //?database?name
          ?? private? String?dbName;

          ?? private?static?final? String?strCreateTestClobTeble?=? "CREATE?TABLE?APP.test?(id?INT,?name?VARCHAR(30),text?CLOB(64?K))" ;

          ?? private?static?final? String?strInsertIntoTestTeble?=? "INSERT?INTO?APP.test?(id,?name)??VALUES?(?,??)" ;
          ?? public?static?final? String?strGetTest?=? "SELECT?*?FROM?APP.test?WHERE?ID?=??" ;
          ?? private?static?final? String?strCreateCourseTable?=? "create?table?APP.Course?("
          ?????? +? "????ID??????????INTEGER?NOT?NULL?PRIMARY?KEY?GENERATED?ALWAYS?AS?IDENTITY?(START?WITH?1,?INCREMENT?BY?1),"
          ?????? +? "????name????VARCHAR(30),?"
          ?????? +? "????description??VARCHAR(30),?"
          ?????? +? "????startDate??DATE,?"? +? "????endDate?????DATE?"? +? ")" ;

          ?? private?static?final? String?strCreateStudentTable?=? "create?table?APP.ADDRESS?("
          ?????? +? "????ID??????????INTEGER?NOT?NULL?PRIMARY?KEY?GENERATED?ALWAYS?AS?IDENTITY?(START?WITH?1,?INCREMENT?BY?1),"
          ?????? +? "????LASTNAME????VARCHAR(30),?"
          ?????? +? "????FIRSTNAME???VARCHAR(30),?"
          ?????? +? "????MIDDLENAME??VARCHAR(30),?"
          ?????? +? "????PHONE???????VARCHAR(20),?"
          ?????? +? "????EMAIL???????VARCHAR(30),?"
          ?????? +? "????ADDRESS1????VARCHAR(30),?"
          ?????? +? "????ADDRESS2????VARCHAR(30),?"
          ?????? +? "????CITY????????VARCHAR(30),?"
          ?????? +? "????STATE???????VARCHAR(30),?"? +? ")" ;

          ?? public? DatabaseUtils()?{
          ???? this ( "test" );
          ?? }

          ?? public? DatabaseUtils(String?dbName)?{
          ???? this .dbName?=?dbName;

          ???? setDBSystemDir();
          ???? dbProperties?=?loadDBProperties();
          ???? String?driverName?=?dbProperties.getProperty( "db.driver" );
          ???? loadDatabaseDriver(driverName);
          ???? if? (!dbExists())?{
          ?????? createDatabase();
          ???? }
          ?? }

          ?? private? Properties?loadDBProperties()?{
          ???? InputStream?dbPropInputStream?=? null ;

          ???? dbPropInputStream?=?DatabaseUtils. class
          ???????? .getResourceAsStream(DB_PROPERTIES_FILE);
          ???? dbProperties?=? new? Properties();

          ???? try? {
          ?????? dbProperties.load(dbPropInputStream);
          ???? }? catch? (IOException?e)?{
          ?????? e.printStackTrace();
          ???? }
          ???? return? dbProperties;
          ?? }

          ?? private?void? setDBSystemDir()?{
          ???? String?userDir?=?System.getProperty( "user.dir" ,? "." );
          ???? String?systemDir?=?userDir?+?DB_OPPOSITE_LOCATION;
          ???? System.setProperty( "derby.system.home" ,?systemDir);

          ???? //?create?the?db?System?dir
          ???? File?fileSystemDir?=? new? File(systemDir);
          ???? fileSystemDir.mkdir();
          ?? }

          ?? private?void? loadDatabaseDriver(String?driverName)?{
          ???? try? {
          ?????? Class.forName(driverName);
          ???? }? catch? (ClassNotFoundException?e)?{
          ?????? e.printStackTrace();
          ???? }
          ?? }

          ?? private?boolean? dbExists()?{
          ???? boolean? bExists?=? false ;
          ???? String?dbLocation?=?getDatabaseLocation();
          ???? File?dbFileDir?=? new? File(dbLocation);
          ???? if? (dbFileDir.exists())?{
          ?????? bExists?=? true ;
          ???? }
          ???? return? bExists;
          ?? }

          ?? private?boolean? createDatabase()?{
          ???? boolean? bCreated?=? false ;
          ???? Connection?dbConnection?=? null ;

          ???? String?dbUrl?=?getDatabaseUrl();
          ???? dbProperties.put( "create" ,? "true" );

          ???? try? {
          ?????? dbConnection?=?DriverManager.getConnection(dbUrl,?dbProperties);
          ?????? bCreated?=?createTables(dbConnection,?strCreateTestClobTeble);
          ???? }? catch? (SQLException?e)?{
          ?????? e.printStackTrace();
          ???? }

          ???? dbProperties.remove( "create" );
          ???? return? bCreated;
          ?? }

          ?? private?boolean? createTables(Connection?dbConnection,?String?creatTableSql)?{
          ???? boolean? bCreatedTables?=? false ;
          ???? Statement?statement?=? null ;

          ???? try? {
          ?????? statement?=?dbConnection.createStatement();
          ?????? statement.execute(creatTableSql);
          ?????? bCreatedTables?=? true ;
          ???? }? catch? (SQLException?e)?{
          ?????? e.printStackTrace();
          ???? }
          ???? return? bCreatedTables;
          ?? }

          ?? public? String?getDatabaseUrl()?{

          ???? return? dbProperties.getProperty( "db.url" )?+?dbName;
          ?? }

          ?? public? String?getDatabaseLocation()?{
          ???? String?dbLocation?=?System.getProperty( "derby.system.home" )?+? "/"
          ???????? +?dbName;
          ???? return? dbLocation;
          ?? }

          ?? public?boolean? connect()?{
          ???? String?dbUrl?=?getDatabaseUrl();
          ???? try? {
          ?????? logger.info( "DBUrl:?"? +?dbUrl);
          ?????? dbConnection?=?DriverManager.getConnection(dbUrl,?dbProperties);
          ?????? isConnected?=?dbConnection?!=? null ;
          ???? }? catch? (SQLException?e)?{
          ?????? //?TODO?Auto-generated?catch?block
          ?????? e.printStackTrace();
          ?????? isConnected?=? false ;
          ?????? logger.info( "create?connection?if?failed!" );
          ???? }
          ???? return? isConnected;
          ?? }

          ?? public? Connection?getConnection()?{
          ???? return? dbConnection;
          ?? }

          ?? public?void? disconnect()?{
          ???? if? (isConnected)?{
          ?????? String?dbUrl?=?getDatabaseUrl();
          ?????? dbProperties.put( "shutdown" ,? "true" );
          ?????? try? {
          ???????? System.out.println( "斷開數據庫連接????????????????" );
          ???????? DriverManager.getConnection(dbUrl,?dbProperties);
          ???????? System.out.println( "????????????????" );
          ?????? }? catch? (SQLException?e)?{
          ???????? //?e.printStackTrace();
          ???????? logger.info( "disconnect?the?connection?Successful!" );
          ?????? }
          ?????? isConnected?=? false ;
          ???? }
          ?? }

          ?? /**
          ??? *? @param? args
          ??? */
          ?? public?static?void? main(String[]?args)?{
          ???? //?TODO?Auto-generated?method?stub
          ???? DatabaseUtils?testdb?=? new? DatabaseUtils();
          ???? logger.info(testdb.getDatabaseLocation());
          ???? logger.info(testdb.getDatabaseUrl());
          ???? testdb.connect();
          ???? Connection?c?=?testdb.getConnection();
          ???? PreparedStatement?ps?=? null ;
          ???? try? {
          ?????? ps?=?c.prepareStatement(DatabaseUtils.strInsertIntoTestTeble,?Statement.RETURN_GENERATED_KEYS);
          ?????? ps.setInt( 1 ,? 1 );
          ?????? ps.setString( 2 ,? "test?Icerain" );
          ?????? int? i?=ps.executeUpdate();
          ?????? System.out.println(i);
          ?????? ps.close();
          ??????
          ?????? ps?=?c.prepareStatement(DatabaseUtils.strGetTest);
          ?????? ps.setInt( 1 ,? 1 );
          ?????? ResultSet?rs?=?ps.executeQuery();
          ?????? if (rs.next())?{
          ???????? String?name?=?rs.getString( 2 );
          ???????? System.out.println(name);
          ?????? }
          ?????? ps.close();
          ???? }? catch? (SQLException?e)?{
          ?????? //?TODO?Auto-generated?catch?block
          ?????? e.printStackTrace();
          ???? }
          ???? testdb.disconnect();
          ?? }

          }

           

          下面是一個插入數據的類 InsertData.java

          package? test.jdbc;

          import? java.sql.Types;

          import? javax.sql.DataSource;

          import? org.springframework.jdbc.core.SqlParameter;
          import? org.springframework.jdbc.object.SqlUpdate;

          public?class? InsertData? extends? SqlUpdate?{
          ?? //?需要注入一個DataSource...
          ?? public? InsertData(DataSource?ds)?{
          ???? setDataSource(ds);?? //?TODO?注意?設置數據源
          ???? setSql( "INSERT?INTO?APP.test?(id,?name)??VALUES?(?,??)" );
          ???? declareParameter( new? SqlParameter(Types.INTEGER));
          ???? declareParameter( new? SqlParameter(Types.VARCHAR));
          ????
          ???? compile(); //?TODO?注意?,?要編譯以后才可以使用
          ?? }
          ??
          ?? //?覆蓋insert方法
          ?? public?int? insert(TestData?data)?{
          ???? Object[]?params?=? new? Object[]?{data.id,data.name};
          ???? return? update(params);?? //?執行插入操作....
          ?? }

          }

          很簡單, 并帶有詳細注釋.

          下面是一個查詢的類 QueryDataById.java

          package? test.jdbc;

          import? java.sql.ResultSet;
          import? java.sql.SQLException;
          import? java.sql.Types;

          import? javax.sql.DataSource;

          import? org.springframework.jdbc.core.SqlParameter;
          import? org.springframework.jdbc.object.MappingSqlQuery;

          public?class? QueryDataById? extends? MappingSqlQuery{
          ?? private?static?final? String?sql?=? "SELECT?*?FROM?APP.test?WHERE?ID?=??" ;
          ?? public? QueryDataById(DataSource?ds)?{
          ???? super (ds,sql);
          ???? declareParameter( new? SqlParameter( "id" ,Types.INTEGER));
          ???? compile();
          ?? }
           

          ? //?覆蓋mapRow方法
          ?? @Override
          ?? protected? Object?mapRow(ResultSet?rs,? int? index)? throws? SQLException?{
          ???? //?TODO?Auto-generated?method?stub
          ???? TestData?tdata?=? new? TestData();
          ???? tdata.id?=?rs.getInt( 1 );
          ???? tdata.name?=?rs.getString( 2 );
          ????
          ???? return? tdata;
          ?? }
          ??
          }

           

          也很簡單.

          注意:

          ?以上兩個類都實現了Spring簡化Jdbc操作的一些接口, 關于接口的信息請查考文檔, 這里不在詳細講述.

           

          下面是一個很簡單的測試(數據)實體類.TestData.java

          package? test.jdbc;

          public?class? TestData?{
          ?? public?int? id;
          ?? public? String?name;
          ?? public? TestData( int? id,?String?name)?{
          ???? this .id?=?id;
          ???? this .name?=?name;
          ?? }
          ?? public? TestData()?{}
          }

          下面是一個測試數據源是否注入正確的類:TestDataSource.java

          package? test.jdbc;

          import? java.sql.Connection;
          import? java.sql.PreparedStatement;
          import? java.sql.ResultSet;

          import? javax.sql.DataSource;

          public?class? TestDataSource?{
          ?? private? DataSource?dataSource;
           

          // 注入數據源
          ?? public?void? setDataSource(DataSource?dataSource)?{
          ???? this .dataSource?=?dataSource;
          ?? }
           

          //測試數據源
          ?? public?void? testDataSource()?{
          ???? try? {
          ?????? System.out.println( "Test?DataSource!!!" );
          ?????? Connection?connection?=?dataSource.getConnection();
          ?????? if? (connection?!=? null )
          ???????? System.out.println( "test?ok!" );
          ??????
          ?????? PreparedStatement?ps?=? null ;
          ?????? ps?=?connection.prepareStatement(DatabaseUtils.strGetTest);
          ?????? ps.setInt( 1 ,? 1 );
          ?????? ResultSet?rs?=?ps.executeQuery();
          ?????? if (rs.next())?{
          ???????? String?name?=?rs.getString( 2 );
          ???????? System.out.println( "測試數據源配置:"? +?name);
          ?????? }
          ?????? ps.close();
          ???? }? catch? (Exception?e)?{
          ?????? e.printStackTrace();
          ???? }
          ?? }

          }

          下面是測試Spring提高的Jdbc功能的主要測試類, 測試了一些使用JDBC操作數據的常用功能, 其他沒有測試的請查看其Doc,TestJdbcTemplate.java

          package? test.jdbc;

          import? java.sql.CallableStatement;
          import? java.sql.PreparedStatement;
          import? java.sql.ResultSet;
          import? java.sql.SQLException;
          import? java.sql.Types;
          import? java.util.List;

          import? org.springframework.dao.DataAccessException;
          import? org.springframework.jdbc.core.BatchPreparedStatementSetter;
          import? org.springframework.jdbc.core.CallableStatementCallback;
          import? org.springframework.jdbc.core.JdbcTemplate;
          import? org.springframework.jdbc.core.RowCallbackHandler;
          import? org.springframework.jdbc.core.RowMapper;
          import? org.springframework.jdbc.core.RowMapperResultSetExtractor;

          public?class? TestJdbcTemplate?{
          ?? public?static?final? String?strGetTest?=? "SELECT?*?FROM?APP.test?WHERE?ID?=??" ;
          ?? private?static?final? String?strInsertIntoTestTeble?=? "INSERT?INTO?APP.test?(id,?name)??VALUES?(?,??)" ;
          ??
          ?? private? JdbcTemplate?jdbcTemplate;
          ??
          ?? public? TestJdbcTemplate()?{}
          ?? public? TestJdbcTemplate(JdbcTemplate?jdbcTemplate)?{
          ???? this .jdbcTemplate?=?jdbcTemplate;
          ?? }

          ?? public? JdbcTemplate?getJdbcTemplate()?{
          ???? return? jdbcTemplate;
          ?? }

          ?? public?void? setJdbcTemplate(JdbcTemplate?jdbcTemplate)?{
          ???? this .jdbcTemplate?=?jdbcTemplate;
          ?? }
          ??
          ?? /**??測試?插入數據*/
          ?? public?void? insertTestData( int? id,String?name)?{
          ???? Object[]?params?=? new? Object[]?{id,name};
          ???? jdbcTemplate.update(strInsertIntoTestTeble,?params);
          ???? System.out.println( "插入數據成功!" );
          ?? }
          ?? /**??測試?插入數據*/
          ?? public?void? insertTestDataWithTypeChecked( int? id,String?name)?{
          ???? Object[]?params?=? new? Object[]?{id,name};
          ???? int []?types?=? new?int []?{Types.INTEGER,Types.VARCHAR};
          ???? jdbcTemplate.update(strInsertIntoTestTeble,?params,?types);
          ???? System.out.println( "插入數據成功(with?Types?checked)!" );
          ?? }
          ?? /**??測試?批量插入數據*/
          ?? public?void? insertTestDataByBatchInsert( final? List<TestData>?datas)?{
          ???? //構造?BatchPreparedStatementSetter
          ???? BatchPreparedStatementSetter?setter?=? new? BatchPreparedStatementSetter()?{
          ?????? public?int? getBatchSize()?{
          ???????? return? datas.size();
          ?????? }
          ?????? public?void? setValues(PreparedStatement?ps,? int? index)?{
          ???????? TestData?data?=?datas.get(index);
          ???????? try? {
          ?????????? ps.setInt( 1 ,?data.id);??? //?從1?開始......
          ?????????? ps.setString( 2 ,?data.name);
          ???????? }? catch? (SQLException?e)?{
          ?????????? //?TODO?Auto-generated?catch?block
          ?????????? e.printStackTrace();
          ???????? }
          ?????? }
          ???? };
          ???? jdbcTemplate.batchUpdate(strInsertIntoTestTeble,?setter);
          ???? System.out.println( "批量插入數據成功!" );
          ?? }
          ?? /**
          ??? *?測試?JdbcTemplate
          ??? *? @param? id
          ??? */
          ?? public?void? getTestData( final?int? id)?{
          ???? final? Object[]?params?=? new? Object[]?{id};
          ????
          ???? jdbcTemplate.query(strGetTest,?params,? new? RowCallbackHandler()?{

          ?????? public?void? processRow(ResultSet?rs)? throws? SQLException?{
          ???????? //?TODO?Auto-generated?method?stub
          ???????? System.out.println( "測試JdbcTemplate::?name:"? +?rs.getString( 2 ));
          ?????? }
          ??????
          ???? });
          ?? }
          ??
          ?? /**?測試RowMapper?和?RowMapperResultReader?接口*/
          ?? public?void? getDataUsingRowMapper( int? id)?{
          ???? List?datas?=?jdbcTemplate.query( "SELECT?*?FROM?APP.test?WHERE?ID?=?"? +?id,? new? RowMapper()?{
          ?????? //?實現RowMapper接口,?來映射每一行數據
          ?????? public? Object?mapRow(ResultSet?rs,? int? index)? throws? SQLException?{
          ???????? //?TODO?Auto-generated?method?stub
          ???????? System.out.println( "測試RowMapper?接口:?name?"? +?rs.getString( 2 ));
          ???????? return?null ;
          ?????? }
          ??????
          ???? });
          ???? //datas?中保存查詢結果
          ???? System.out.println(datas.size());
          ?? }
          ??
          ?? public? TestData?getDataUsingRowMapperResultReader( final?int? id)?{
          ???? final? Object[]?params?=? new? Object[]?{id};
          ???? //?TODO?有問題.......
          ???? TestData?data?=??(TestData)?jdbcTemplate.query(strGetTest,params,new?RowMapperResultSetExtractor( new? RowMapper()?{
          ?????? public? Object?mapRow(ResultSet?rs,int?index)? throws? SQLException?{
          ???????? TestData?tdata?=? new? TestData();
          ???????? tdata.id?=?rs.getInt( 1 );
          ???????? tdata.name?=?rs.getString( 2 );
          ???????? return? tdata;
          ?????? }
          ???? }));
          ????
          ???? return? data;
          ?? }
          ??
          ?? /**?測試調用存儲過程..*/
          ?? public?void? testCallableStatement()?{
          ???? //?使用?CallableStatementCallback?回調接口?調用存儲過程.
          ???? CallableStatementCallback?cb?=? new? CallableStatementCallback()?{
          ?????? public? Object?doInCallableStatement(CallableStatement?cs)? throws? SQLException,?DataAccessException?{
          ???????? cs.execute();
          ???????? return?null ;
          ?????? }
          ???? };
          ???? //?GET_DATA?為存儲過程名
          ???? jdbcTemplate.execute( "{?GET_DATA}" ,?cb);
          ?? }
          ??
          ??
          ?? //?用對象操作數據,?使用SqlUpdate接口.?見?InsertData?類,,,,,
          ?? //?有容器注入?InsertData.
          ?? private? InsertData?insertData?;
          ?? public?int? insertTestData(TestData?data)?{
          ???? return? insertData.insert(data);
          ?? }
          ?? public? InsertData?getInsertData()?{
          ???? return? insertData;
          ?? }
          ?? public?void? setInsertData(InsertData?insertData)?{
          ???? this .insertData?=?insertData;
          ?? }
          ?? //測試插入數據
          ?? public?void? insertDataUsingSqlUpdate(TestData?data)?{
          ???? insertData.insert(data);
          ???? System.out.println( "使用SqlUpdate接口插入數據?成功....." );
          ?? }
          ??
          ?? /**?和上面使用SqlUpdate接口一樣?把操作創建為對象來操作*/
          ?? private? QueryDataById?queryDataById;

          ?? public?void? setQueryDataById(QueryDataById?queryDataById)?{
          ???? this .queryDataById?=?queryDataById;
          ?? }
          ?? //測試
          ?? public? TestData?getDataUsingMappingSqlQuery( int? id)?{
          ???? Object[]?params?=? new? Object[]?{id};
          ???? return? (TestData)?queryDataById.execute(params).get( 0 );
          ?? }
          ?? //使用上面兩種方法來插入和查詢數據,不用和JDBC?API交互,?有Spring提供了
          ?? //中間代理層
          ??
          }

          下面是函有main函數的 主類. TestApp.java

          package? test.jdbc;

          import? java.util.ArrayList;
          import? java.util.List;

          import? org.springframework.context.ApplicationContext;
          import? org.springframework.context.support.ClassPathXmlApplicationContext;

          public?class? TestApp?{

          ?? /**
          ??? *? @param? args
          ??? */
          ?? public?static?void? main(String[]?args)?{
          ???? DatabaseUtils?dataUtils?=? new? DatabaseUtils();
          ???? dataUtils.connect();
          ???? System.out.println( "Open?database:!" );
          ???? //?TODO?Auto-generated?method?stub
          ???? //測試連接數據源?......
          ???? ApplicationContext?context?=? new? ClassPathXmlApplicationContext( "test/jdbc/spring-traning.xml" );
          ???? /*?測試?DataSource?配置*/
          ???? TestDataSource?ds?=?(TestDataSource)?context.getBean( "dataBean" );
          ???? ds.testDataSource();

          ???? /*?測試JdbcTemplate?配置*/
          ????
          ???? TestJdbcTemplate?tjt?=?(TestJdbcTemplate)?context.getBean( "testJdbcTemplate" );
          ???? tjt.insertTestData( 2 ,? "test?name2" );
          ???? tjt.insertTestDataWithTypeChecked( 3 ,? "test?name?3" );
          ???? List<TestData>?datas?=? new? ArrayList<TestData>();
          ???? datas.add( new? TestData( 4 , "test?name?4" ));
          ???? datas.add( new? TestData( 5 , "test?name?5" ));
          ???? datas.add( new? TestData( 6 , "test?name?6" ));
          ???? tjt.insertTestDataByBatchInsert(datas);
          ???? tjt.getTestData( 1 );
          ???? tjt.getTestData( 2 );
          ???? tjt.getTestData( 5 );
          ???? tjt.getDataUsingRowMapper( 5 );
          //TODO?類型轉換錯誤.????TestData?data?=?tjt.getDataUsingRowMapperResultReader(1);
          //????System.out.println("測試使用?RowMapperResultSetExtractor?讀取一行數據:?"?+?data.id?+?":::?name?"?+?data.name);
          ???? TestData?tdata?=? new? TestData( 9 , "TestSqlUpdate." );
          ???? tjt.insertDataUsingSqlUpdate(tdata);?? //?插入數據
          ????
          ???? tjt.getDataUsingRowMapper( 9 );? //?測試上面插入的數據是否成功
          ????
          ????
          ???? dataUtils.disconnect();
          ?? }

          }

           

          JDBC配置文件:jdbc.properties

          # Sample ResourceBundle properties file

          db.username=

          addressuser

          db.password=

          addressuser

          db.driver=

          org.apache.derby.jdbc.EmbeddedDriver

          db.url=

          jdbc : derby:

          db.table=

          test

          db.schema=

          APP

          db.urlName=

          jdbc : derby:test

           

          最后是最重要的配置文件: spring-traning.xml

          <?

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

          <!

          DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

          <

          beans >

          < bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

          < property name = "location" >

          < value > test/jdbc/jdbc.properties </ value >

          </ property >

          </ bean >

          <!-- get dataSource,配置dataSource -->

          <!-- 從JNDI得到DataSource -->

          <!-- bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

          <property name="jndiName">

          <value>java:comp/env/jdbc/myDataSource</value>

          </property>

          </bean> -->

          <!-- 使用Spring中的 DriverManagerDataSource -->

          <!-- bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

          <property name="driverClassName">

          <value>${db.driver}</value>

          </property>

          <property name="url">

          <value>${db.urlName}</value>

          </property>

          <property name="username">

          <value>${db.username}</value>

          </property>

          <property name="password">

          <value>${db.password}</value>

          </property>

          </bean -->

          <!-- 使用DBCP dataSource -->

          < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" >

          < property name = "driverClassName" >

          < value > ${db.driver} </ value >

          </ property >

          < property name = "url" >

          < value > ${db.urlName} </ value >

          </ property >

          < property name = "username" >

          < value > ${db.username} </ value >

          </ property >

          < property name = "password" >

          < value > ${db.password} </ value >

          </ property >

          </ bean >

          < bean id = "dataBean" class = "test.jdbc.TestDataSource" >

          < property name = "dataSource" >

          < ref bean = "dataSource" />

          </ property >

          </ bean >

          <!-- ################################################################# -->

          <!-- 配合DataSource測試JdbcTemplate -->

          < bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" >

          < property name = "dataSource" >

          < ref bean = "dataSource" />

          </ property >

          </ bean >

          < bean id = "testJdbcTemplate" class = "test.jdbc.TestJdbcTemplate" >

          < constructor-arg >

          < ref bean = "jdbcTemplate" />

          </ constructor-arg >

          <!-- 測試 SqlUpdate 接口... -->

          < property name = "insertData" >

          < ref bean = "insertDataUsingSqlUptate" />

          </ property >

          </ bean >

          <!-- 測試SqlUpdate......接口 -->

          < bean id = "insertDataUsingSqlUptate" class = "test.jdbc.InsertData" >

          < constructor-arg >

          < ref bean = "dataSource" />

          </ constructor-arg >

          </ bean >

          </

          beans >

           

          ok, Jdbc測試的代碼就結束了.

          在這里主要學習了,Spring提高的使用Jdbc的一些包裝類和接口, 來更方便的使用Jdbc操作數據, 不用些那么一些煩人的 try ... catch...... finally.....

          感覺使用Sprig效率是很好,代碼看起來也很優美哦. 呵呵:

          ok ,今天就終結完了, 下一次看看在Spring中如何高效使用Hibernate吧, 下次見啦:

          posted on 2006-05-25 00:04 冰雨 閱讀(2861) 評論(1)  編輯  收藏 所屬分類: Spring

          Feedback

          # Spring in Action 中綁定 2007-05-28 23:32 香蕉
          我想知道關于Spring in Action 中關于綁定這一部分的學習資料.謝謝各位,本人很急用的,在Spring in Action 這本書中關于綁定這一部分的內容,
          這部分胡代碼是什么?
          什么是綁定?
          如何綁定?
          知道請發送郵箱: sb.475275091@yahoo.com.cn  回復  更多評論
            


          JSF中文技術文摘
          主站蜘蛛池模板: 堆龙德庆县| 五常市| 巩义市| 垫江县| 呼玛县| 普兰店市| 任丘市| 牡丹江市| 清远市| 潍坊市| 铜陵市| 顺平县| 含山县| 乌什县| 临泽县| 兴山县| 陈巴尔虎旗| 新宾| 大悟县| 台北市| 锦州市| 白朗县| 台中县| 仁寿县| 囊谦县| 江津市| 宝兴县| 德令哈市| 长治市| 资溪县| 南昌市| 嘉祥县| 广水市| 民乐县| 石河子市| 工布江达县| 龙胜| 贵阳市| 金乡县| 宁化县| 阿图什市|