|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。存储历程测试spring的存储历程
spring对存储历程举行封装.它的完成细节与jdbc相似
上面举行测试
1):写存储历程实行类:packagejdbc;importjava.sql.Types;importjava.util.HashMap;importjava.util.Map;importjavax.sql.DataSource;importorg.springframework.jdbc.core.SqlParameter;importorg.springframework.jdbc.object.StoredProcedure;importorg.springframework.jdbc.core.SqlOutParameter;importorg.springframework.jdbc.core.SqlParameter;
publicclassStoreTemplateextendsStoredProcedure{
HashMapmap=newHashMap();publicStoreTemplate(){super();
}
publicvoidsetValue(Stringkey,Objectobj){map.put(key,obj);}
publicStoreTemplate(DataSourceds){setDataSource(ds);}publicMapexecute(){if(this.getSql()==null||this.getSql().equals(""))returnnull;this.compile();returnexecute(map);}
publicvoidsetVarcharParam(Stringparam){this.declareParameter(newSqlParameter(param,Types.VARCHAR));}publicvoidsetDoubleParam(Stringparam){this.declareParameter(newSqlParameter(param,Types.DOUBLE));}publicvoidsetIntegerParam(Stringparam){this.declareParameter(newSqlParameter(param,Types.INTEGER));}publicvoidsetVarcharOutParam(Stringparam){this.declareParameter(newSqlOutParameter(param,Types.VARCHAR));}publicvoidsetDoubleOutParam(Stringparam){this.declareParameter(newSqlOutParameter(param,Types.DOUBLE));}publicvoidsetIntegerOutParam(Stringparam){this.declareParameter(newSqlOutParameter(param,Types.INTEGER));}}
2):用spring设置数据源:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans><beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"><value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value></property><propertyname="url"><value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs;SelectMethod=Cursor</value></property><propertyname="username"><value>sa</value></property><propertyname="password"><value></value></property></bean></beans>
3):写junit:packagejdbc;importjunit.framework.*;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.FileSystemXmlApplicationContext;importjava.util.List;importjava.util.Map;importjavax.sql.DataSource;publicclassTestStoreTemplateextendsTestCase{ApplicationContextctx=null;protectedvoidsetUp()throwsException{ctx=newFileSystemXmlApplicationContext("D:workjpetstorecjdbcContext-jdbc.xml");}publicvoidtestStore(){DataSourcedatasource=(DataSource)ctx.getBean("dataSource");StoreTemplateqry=newStoreTemplate(datasource);qry.setSql("testsp");qry.setIntegerParam("count");qry.setIntegerOutParam("ret");qry.setValue("count",newInteger(1));Mapmap=qry.execute();if(map!=null){System.out.println(map.get("ret"));}}
protectedvoidtearDown()throwsException{
}
}
4):写测试存储历程SETQUOTED_IDENTIFIERONGOSETANSI_NULLSONGOcreateproceduretestsp(@countint,@retintout)asbeginselect@ret=@count+1endGOSETQUOTED_IDENTIFIEROFFGOSETANSI_NULLSONGO
5):编译运转ok.
6):附jdbc挪用存储历程的办法
packagejdbc;
importjunit.framework.*;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.FileSystemXmlApplicationContext;
importjavax.sql.DataSource;importjava.sql.*;
publicclassTestJdbcCallStoreextendsTestCase{ApplicationContextctx=null;protectedvoidsetUp()throwsException{ctx=newFileSystemXmlApplicationContext("D:workjpetstorecjdbcContext-jdbc.xml");}
publicvoidtestStore(){DataSourcedatasource=(DataSource)ctx.getBean("dataSource");CallableStatementcstmt=null;try{cstmt=datasource.getConnection().prepareCall("{calltestsp(?,?)}");cstmt.setInt(1,1);cstmt.registerOutParameter(2,Types.INTEGER);cstmt.executeUpdate();Objectobj=cstmt.getObject(2);if(obj!=null){System.out.println(obj.toString());}}catch(SQLExceptiones){es.printStackTrace(System.out);}finally{
}}
protectedvoidtearDown()throwsException{
}
}
//为了复杂,有些代码省往了,象transaction.
mzhanker@126.com
Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。Oracle收购Sun后Java前途未卜。 |
|