请教为何spring注入Oraclespring4 lobhandlerr实例不成功

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&5457人阅读
Spring技术(4)
所谓CLOB 可以看成是文本文,所谓BLOB可以看成是图片文件
假设在mysql数据库上有以下表:
create table test(id int primary key,txt TEXT,image BLOB);
//写入&假设现在分别读取一个文字文件和二进制文件,并想将之存储到数据库中,则可以使用JdbcTemplate 如:
final File binaryFile=new File("wish.jpg");final File txtFile=new File("test.txt");final InputStream is=new FileInputStream(binaryFile);final Reader reader=new FileReader(txtFile);
JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);final LobHandler lobHandler=new DefaultLobHandler();jdbcTemplate.execute("insert into test (txt,image) values (?,?)",&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& new AbstractLobCreatingPreparedStatementCallBack(lobHandler)...{ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& protected void setValues(PreoparedStatement pstmt,LobCreator lobCreator)...{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& lobCreator.setClobAsCharactoerStream(pstmt,1,reader,(int)textFile.length());&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& lobCreator.setBlobAsBinaryStream(pstmt,2,is,(int)binaryFile.length());&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& });reader.close();is.close();
//读取在建立AbstractLobCreatingPreparedStatementCallBack对象时候,需要一个lobHandler实例, 对于一般的数据库,采用DefaultLobHandler足以,对于Oracle特定的lob处理,可以使用OracleLobHandler
如果是讲数据从数据库中读取出来并另存在未见,可以使用下面的程序
final Writer writer=new FileWriter("test_back.txt");final OutputStream os=new FileOutputStream(new File("wish_bak.jpg"));jdbcTemplate.query("select txt,image from test where id=?,new AbstractLobStreamingResultSetExtractor(){&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& protected void streamData(ResultSet rs) throws SQLException,IOException,DataAccessException...{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& FileCopyUtils.copy(lobHandler.getClobAsCharacterStream(rs,1),writer);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs,2),os);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& });writer.close();os.close();这里使用FileCopyUtils的copy方法,将lobHandler取得的串流直接转接给文件输出FileWriter,FileOutputStream对象
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:131916次
积分:1849
积分:1849
排名:第14347名
原创:66篇
转载:10篇
(1)(1)(1)(3)(7)(8)(7)(1)(2)(1)(2)(7)(12)(23)java(57)
先来赞一句,Spring果然是博大精深,急程序员所急,想程序员所想,&#20540;得推荐。
就拿Oracle中的lob字段处理来说,大家都知道这是一个比较麻烦的事, 在加上Hibernate之后处理起来也不是优美(感谢czf的文档), 但是有了Spring一切都是简单解决。
只需要在配置文件中简单几行,代码无需多写一行,就解决了。
在Spring的文件中作如下配置:
&bean id=&nativeJdbcExtractor& class=&org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor&/&
&&& &bean name=&oracleLobHandler& class=&org.springframework.jdbc.support.lob.OracleLobHandler& lazy-init=&true&&
&&&&&&& &property name=&nativeJdbcExtractor&&
&&&&&&&&&&& &ref local=&nativeJdbcExtractor&/&
&&&&&&& &/property&
&&& &/bean&
&bean id=&sessionFactory& class=&org.springframework.orm.hibernate3.LocalSessionFactoryBean&&
&&&&&&& &property name=&dataSource&&
&&&&&&&&&&& &ref bean=&dataSource&/&
&&&&&&& &/property&
&&&&&&& &property name=&lobHandler&&
&&&&&&&&&&& &ref bean=&oracleLobHandler&&&/ref&
&&&&&&& &/property&
&property name=&content& column=&Content& type=&org.springframework.orm.hibernate3.support.ClobStringType&&&/property&
这样就搞定了。。。
注意事项:
1. 在Oralce使用需要注意JDBC Driver的版本
2. 在DAO中加上事务管理。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:27998次
排名:千里之外
转载:101篇
(1)(2)(1)(2)(2)(2)(1)(2)(2)(2)(1)(2)(4)(2)(2)(1)(2)(7)(29)(37)(4)(2)Spring 3.x企业应用开发实战(13)----Spring JDBC访问数据库
Spring JDBC是Spring所提供的持久层技术。他的目的是降低JDBC API门槛,以一种更直接、更简洁的方式使用JDBC API。
JdbcTemplate小试牛刀
package com.smart. &
import java.sql.CallableS &
import java.sql.C &
import java.sql.PreparedS &
import java.sql.ResultS &
import java.sql.SQLE &
import java.sql.T &
import java.util.HashM &
import java.util.L &
import java.util.M &
import org.springframework.beans.factory.annotation.A &
import org.springframework.dao.DataAccessE &
import org.springframework.jdbc.core.BatchPreparedStatementS &
import org.springframework.jdbc.core.CallableStatementC &
import org.springframework.jdbc.core.CallableStatementC &
import org.springframework.jdbc.core.CallableStatementCreatorF &
import org.springframework.jdbc.core.JdbcT &
import org.springframework.jdbc.core.PreparedStatementC &
import org.springframework.jdbc.core.RowCallbackH &
import org.springframework.jdbc.core.RowM &
import org.springframework.jdbc.core.SqlOutP &
import org.springframework.jdbc.core.SqlP &
import org.springframework.jdbc.support.GeneratedKeyH &
import org.springframework.jdbc.support.KeyH &
import org.springframework.stereotype.R &
import com.smart.domain.U &
@Repository &
public class ViewSpaceDao &&
& & @Autowired &
& & private JdbcTemplate jdbcT &
& & public void initDb() &
& & & & String sql=&create table t_user_2(user_id int primary key,user_name varchar(60))&; &
& & & & jdbcTemplate.execute(sql); &
& & public void updateJdbc1() &
& & & & final String sql=&INSERT INTO t_user(user_name,password) VALUES(?,?)&; &
& & & & Object[] params=new Object[]{&MonkeyKing&,&O悟空&}; &
& & & & jdbcTemplate.update(sql,params); &
& & public void updateJdbc2() &
& & & & final String sql=&INSERT INTO t_user(user_name,password) VALUES(?,?)&; &
& & & & Object[] params=new Object[]{&MonkeyKing&,&O悟空&}; &
& & & & jdbcTemplate.update(sql,params,new int[]{Types.VARCHAR,Types.VARCHAR}); &
& & //@得主I的更新 &
& & public &void updateJdbc3() &
& & & & final String sql=&INSERT INTO t_user(user_name,password) VALUES(?,?)&; &
& & & & &&
& & & & KeyHolder keyHolder=new GeneratedKeyHolder();//建一主I逃 & & &&
& & & & jdbcTemplate.update(new PreparedStatementCreator() { & & & & & & &
& & & & & & @Override &
& & & & & & public PreparedStatement createPreparedStatement(Connection arg0) &
& & & & & & & & & & throws SQLException { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & PreparedStatement ps=arg0.prepareStatement(sql); &
& & & & & & & & ps.setString(1, &MonkeyKing&); &
& & & & & & & & ps.setString(2, &O悟空&); &
& & & & & & & & &
& & & & & & } &
& & & & },keyHolder); &
& & & & //@得主I并打印出主I &
& & & & System.out.println(keyHolder.getKey().intValue()); &
& & & & //jdbcTemplate. &
& & //批量更改 &
& & public void batchUpdateSql(final List&User& users) &
& & & & final String sql=&INSERT INTO t_user(user_name,password) VALUES(?,?)&; &
& & & & jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() { &
& & & & & & &&
& & & & & & @Override &
& & & & & & public void setValues(PreparedStatement arg0, int arg1) throws SQLException { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & User user=users.get(arg1); &
& & & & & & & & arg0.setString(1,user.getUserName()); &
& & & & & & & & arg0.setString(2, user.getPassword()); &
& & & & & & } &
& & & & & & &&
& & & & & & @Override &
& & & & & & public int getBatchSize() { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & return users.size(); &
& & & & & & } &
& & & & }); &
& & //查l,多l只需修改返回型和一些定x型 &
& & public User getDataSql(final int userId) &
& & & & String sql=&SELECT user_name,password from t_user where user_id=?&; &
& & & & final User user=new User(); &
& & & & jdbcTemplate.query(sql, new Object[]{userId},new RowCallbackHandler(){ &
& & & & & & @Override &
& & & & & & public void processRow(ResultSet rs) throws SQLException { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & user.setUserId(userId); &
& & & & & & & & user.setUserName(rs.getString(&user_name&)); &
& & & & & & & & user.setPassword(rs.getString(&password&)); &
& & & & & & } & & & & & &&
& & & & }); & & &&
& & //使用RowMapper&T&斫Y果集 &
& & public List&User& getUserById(final int startId,final int endId) &
& & & & String sql=& SELECT user_name,password from t_user where user_id between ? and ?&; &
& & & & &&
& & & & return jdbcTemplate.query(sql, new Object[]{startId,endId}, new RowMapper&User&(){ &
& & & & & & @Override &
& & & & & & public User mapRow(ResultSet rs, int arg1) throws SQLException { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & User user=new User(); &
& & & & & & & & user.setUserName(rs.getString(&user_name&)); &
& & & & & & & & user.setPassword(rs.getString(&password&)); & & & & & & &&
& & & & & & & & &
& & & & & & } & & & & & &&
& & & & }); &
& & //{用存^程 &
& & public int getUserNum(final int userId) &
& & & & String sql=&{call P_GET_USER(?,?)}&;//#1、{用存^程的SQLZ句 & & & &&
& & & & Integer num=jdbcTemplate.execute(sql, new CallableStatementCallback&Integer&() { &
& & & & & & @Override &
& & & & & & public Integer doInCallableStatement(CallableStatement cs) &
& & & & & & & & & & throws SQLException, DataAccessException { &
& & & & & & & & cs.setInt(1, userId);//#2、定入 &
& & & & & & & & cs.registerOutParameter(2, Types.INTEGER);//#3、]暂出 &
& & & & & & & & cs.execute(); &
& & & & & & & & return cs.getInt(2);//#4、@取出档闹 &
& & & & & & } & & & & & &&
& & & & }); &
& & //{用存^程 &
& & public int getUserNum2(final int userId) &
& & & & String sql = &{call P_GET_USER(?,?)}&;// #1、{用存^程的SQLZ句 &
& & & & //通^CallableStatementCreatorFactory建CallableStatementCreator &
& & & & CallableStatementCreatorFactory fac=new CallableStatementCreatorFactory(sql); &
& & & & fac.addParameter(new SqlParameter(&userId&, Types.INTEGER));//入,userId表示名,可以是任意名 &
& & & & fac.addParameter(new SqlOutParameter(&endId&,Types.INTEGER));//出 &
& & & & Map&String,Integer& paramsMap=new HashMap&String, Integer&(); &
& & & & paramsMap.put(&userId&, userId); &
& & & & CallableStatementCreator csc=fac.newCallableStatementCreator(paramsMap);//建一CallableStatementCreator例 &
& & & & Integer num=jdbcTemplate.execute(csc, new CallableStatementCallback&Integer&() { &
& & & & & & @Override &
& & & & & & public Integer doInCallableStatement(CallableStatement cs) &
& & & & & & & & & & throws SQLException, DataAccessException { &
& & & & & & & & cs.execute(); &
& & & & & & & & return cs.getInt(2); &
& & & & & & } & & & & & &&
& & & & }); &
& & & & & & &&
[] view plaincopyprint?
&!-- 扫描类包,将标注Spring注解的类自动转化成Bean,同时完成Bean的注入 --& &
& & &context:component-scan base-package=&com.smart.dao&/& &
& & &!-- 定义一个使用DBCP实现的数据源 --& &
& & &bean id=&dataSource& class=&mons.dbcp.BasicDataSource& destroy-method=&close& p:driverClassName=&com..jdbc.Driver& &&
& & & & p:url=&jdbc:mysql://localhost:3306/sampledb& &
& & & & p:username=&root& &
& & & & p:password=&&/& & &
& & &!-- 定义JDBC模板Bean --& &
& & &bean id=&jdbcTemplate& class=&org.springframework.jdbc.core.JdbcTemplate& &&
& & & & & p:dataSource-ref=&dataSource&/&&!-- 这里将dataSource注入到JdbcTemplate中 --& &
& & &!-- JdbcTemplate Bean将通过@Autowired自动注入到LoginLog和UserDao的Bean中 --& &
& & &!-- 配置事物管理器 --& &
& & &bean id=&transactionManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager& &&
& & & & & p:dataSource-ref=&dataSource&/& &
& & &bean id=&nativeJdbcExtractor& class=&org.springframework.jdbc.monsDbcpNativeJdbcExtractor& lazy-init=&true&/& &
& & &!-- O置本地JDBCο蟪槿 --& &
& & &bean id=&lobHandler& class=&org.springframework.jdbc.support.lob.LobHandler& &&
& & & & & & lazy-init=&true& &&
& & & & & & p:nativeJdbcExtractor-ref=&nativeJdbcExtractor&/& &
& & & & & &&
& & &!-- 通过AOP配置提供事物增强,让Service包下所有Bean的所有方法拥有事物 --& &
& & &aop:config proxy-target-class=&true&& &
& & & & &aop:pointcut expression=&execution(* com.techman.service..*(..))& id=&serviceMethod&/& &
& & & & &aop:advisor advice-ref=&txAdvice& pointcut-ref=&serviceMethod&/& &
& & &/aop:config& &
& & &tx:advice id=&txAdvice& transaction-manager=&transactionManager&& &
& & & & &tx:attributes& &
& & & & & & &tx:method name=&&/& &
& & & & &/tx:attributes& &
& & &/tx:advice& &
BLOB/CLOB类型数据操作
package com.smart. &
import java.io.IOE &
import java.io.InputS &
import java.io.OutputS &
import java.sql.PreparedS &
import java.sql.ResultS &
import java.sql.SQLE &
import java.util.L &
import org.springframework.beans.factory.annotation.A &
import org.springframework.dao.DataAccessE &
import org.springframework.jdbc.LobRetrievalFailureE &
import org.springframework.jdbc.core.JdbcT &
import org.springframework.jdbc.core.RowM &
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementC &
import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetE &
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueI &
import org.springframework.jdbc.support.lob.LobC &
import org.springframework.jdbc.support.lob.LobH &
import org.springframework.stereotype.R &
import org.springframework.util.FileCopyU &
import com.smart.domain.ViewP &
@Repository &
public class ViewPointDao &&
& & @Autowired &
& & private JdbcTemplate jdbcT & &
& & @Autowired &
& & private LobHandler lobH//定xLobHandler傩 &
& & @Autowired &
& & private DataFieldMaxValueI &
& & //添加Lob字段 &
& & public void addViewPoint(final ViewPoint viewPoint) &
& & & & String sql=&INSERT INTO t_view_point(point_id,space_id,point_name,ticket_price,img_file,description) VALUES(?,?,?,?,?,?)&; &
& & & & &&
& & & & jdbcTemplate.execute(sql,new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) { &
& & & & & & &&
& & & & & & @Override &
& & & & & & protected void setValues(PreparedStatement ps, LobCreator lobCreator) &
& & & & & & & & & & throws SQLException, DataAccessException { &
& & & & & & & & &&
& & & & & & & & ps.setInt(1,incre.nextIntValue());//通^自增I指定主I值LobHandler傩 &
& & & & & & & & ps.setInt(2, viewPoint.getSpaceId()); &
& & & & & & & & ps.setString(3, viewPoint.getPointName()); &
& & & & & & & & ps.setDouble(4, viewPoint.getTicketPrice()); &
& & & & & & & & lobCreator.setBlobAsBytes(ps, 5, viewPoint.getImgFile());//O置Blob字段 &
& & & & & & & & lobCreator.setClobAsString(ps, 6, viewPoint.getDescription());//O置Clob字段 &
& & & & & & } &
& & & & }); &
& & //以K绞阶x取Lob &
& & public List&ViewPoint& getImgFiles(final int spaceId) &
& & & & String sql=&SELECT point_id,img_file FROM t_view_point WHERE point_id=? and img_file is not null &; &
& & & & return jdbcTemplate.query(sql, new Object[]{spaceId},new RowMapper&ViewPoint&(){ &
& & & & & & @Override &
& & & & & & public ViewPoint mapRow(ResultSet rs, int rowNum) &
& & & & & & & & & & throws SQLException { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & int pointId=rs.getInt(1); & & & & & & & &&
& & & & & & & & byte[]attach=lobHandler.getBlobAsBytes(rs, 2); &
& & & & & & & & ViewPoint viewPoint=new ViewPoint(); &
& & & & & & & & viewPoint.setPointId(pointId); &
& & & & & & & & viewPoint.setImgFile(attach); & & & & & & & &&
& & & & & & & & return viewP &
& & & & & & } &
& & & & & & &&
& & & & }); &
& & //以流绞阶x取Lob &
& & public void getImgFile(final int pointId,final OutputStream os) &
& & & & String sql=& SELECT img_file FROM t_view_point where point_id=? &; &
& & & & jdbcTemplate.query(sql, new Object[]{pointId},new AbstractLobStreamingResultSetExtractor() { &
& & & & & & &&
& & & & & & protected void handleNoRowFound()throws LobRetrievalFailureException &
& & & & & & { &
& & & & & & & & System.out.println(&Not Found Result!&); &
& & & & & & } &
& & & & & & @Override &
& & & & & & protected void streamData(ResultSet arg0) throws SQLException, IOException, &
& & & & & & & & & & DataAccessException { &
& & & & & & & & // TODO Auto-generated method stub &
& & & & & & & & InputStream is=lobHandler.getBlobAsBinaryStream(arg0, 1); &
& & & & & & & & if(is!=null) &
& & & & & & & & { &
& & & & & & & & & & FileCopyUtils.copy(is, os); &
& & & & & & & & } & & & & & & & &&
& & & & & & } &
& & & & }); &
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 spring 接口注入 实例 的文章

 

随机推荐