亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1117 | 回復(fù): 0
打印 上一主題 下一主題

測試spring的存儲(chǔ)過程 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2007-08-07 20:43 |只看該作者 |倒序?yàn)g覽

測試spring的存儲(chǔ)過程
spring對(duì)存儲(chǔ)過程進(jìn)行封裝.它的實(shí)現(xiàn)細(xì)節(jié)與jdbc類似
下面進(jìn)行測試
1):寫存儲(chǔ)過程執(zhí)行類:
package jdbc;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
public class StoreTemplate
    extends StoredProcedure {
    HashMap map = new HashMap();

    public StoreTemplate() {
super();
    }
    public void setValue(String key, Object obj) {
map.put(key, obj);
    }
    public StoreTemplate(DataSource ds) {
setDataSource(ds);
    }
  
    public Map execute() {
        if(this.getSql()==null || this.getSql().equals("")) return null;
this.compile();
return execute(map);
    }
    public void setVarcharParam(String param) {
this.declareParameter(new SqlParameter(param, Types.VARCHAR));
    }
  
    public void setDoubleParam(String param) {
this.declareParameter(new SqlParameter(param, Types.DOUBLE));
    }
  
    public void setIntegerParam(String param) {
this.declareParameter(new SqlParameter(param, Types.INTEGER));
    }
  
    public void setVarcharOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.VARCHAR));
    }
  
    public void setDoubleOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.DOUBLE));
    }
    public void setIntegerOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.INTEGER));
    }
}
2):用spring配置數(shù)據(jù)源:
      
  com.microsoft.jdbc.sqlserver.SQLServerDriver
  jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs;SelectMethod=Cursor
  sa
  

3):寫junit:
package jdbc;
import junit.framework.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
public class TestStoreTemplate extends TestCase {
ApplicationContext ctx=null;
protected void setUp() throws Exception {
      ctx= new FileSystemXmlApplicationContext("D:\\work\\jpetstore\\src\\jdbc\\Context-jdbc.xml");
}
public void testStore(){
     DataSource datasource=(DataSource)ctx.getBean("dataSource");
     StoreTemplate qry=new StoreTemplate(datasource);
     qry.setSql("testsp");
     qry.setIntegerParam("count");
     qry.setIntegerOutParam("ret");
     qry.setValue("count",new Integer(1));
     Map map=qry.execute();
     if(map!=null){
  System.out.println(map.get("ret"));
     }
}
   
protected void tearDown() throws Exception {
}
}
4):寫測試存儲(chǔ)過程
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create  procedure testsp(@count  int,@ret int out)
as
begin
select @ret=@count+1
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
5):編譯運(yùn)行 ok.
6):附j(luò)dbc調(diào)用存儲(chǔ)過程的方法
package jdbc;
import junit.framework.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.*;
public class TestJdbcCallStore
    extends TestCase {
    ApplicationContext ctx = null;
    protected void setUp() throws Exception {
ctx = new FileSystemXmlApplicationContext("D:\\work\\jpetstore\\src\\jdbc\\Context-jdbc.xml");
    }
    public void testStore() {
DataSource datasource = (DataSource) ctx.getBean("dataSource");
CallableStatement cstmt = null;
try {
     cstmt = datasource.getConnection().prepareCall(
  "{call testsp(?,?)}");
     cstmt.setInt(1, 1);
     cstmt.registerOutParameter(2, Types.INTEGER);
     cstmt.executeUpdate();
     Object obj = cstmt.getObject(2);
     if (obj != null) {
  System.out.println(obj.toString());
     }
}
catch (SQLException es) {
     es.printStackTrace(System.out);
}
finally {
}
    }
    protected void tearDown() throws Exception {
    }
}

//為了簡單,有些代碼省去了,象transaction.


本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u/15511/showart_355605.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP