两种方法测试spring中的jdbc
2024-07-21 02:14:12
供稿:网友
两种方法测试spring中的jdbc
jdbc是一个非常基础的数据存取api,spring对其进行简单的封装, 下面以sqlserver中自带的pubs数据库authors表进行测试.
1):编写authors.java,其每个对象对应于数据库中的一条记录
package jdbc;public class authors { string lname=null; string fname=null; string phone=null; string address=null; public string getaddress() { return address; } public string getfname() { return fname; } public string getlname() { return lname; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public void setlname(string lname) { this.lname = lname; } public void setfname(string fname) { this.fname = fname; } public void setaddress(string address) { this.address = address; }}
2):编写authorsquery类
package jdbc;import java.sql.resultset;import java.sql.sqlexception;import javax.sql.datasource;import org.springframework.jdbc.object.mappingsqlquery;import javax.sql.datasource;
/** * <p>title: 测试spring中的jdbc</p> * <p>description: </p> * <p>copyright: copyright (c) 2004</p> * <p>company: </p> * @author hanker * @version 1.0 */
public class authorsquery extends mappingsqlquery {
public authorsquery(datasource ds) { this.setdatasource(ds); this.setsql("select * from authors"); compile(); }
protected object maprow(resultset rs, int rownum) throws sqlexception {
authors po = new authors(); po.setlname(rs.getstring("au_lname")); po.setfname(rs.getstring("au_fname")); po.setphone(rs.getstring("phone")); po.setaddress(rs.getstring("address")); return po; }
}
3:写spring的配置文件
<?xml version="1.0" encoding="utf-8"?><!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - application context definition for petclinic on hibernate. --><beans> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname"><value>com.microsoft.jdbc.sqlserver.sqlserverdriver</value></property> <property name="url"><value>jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs;selectmethod=cursor</value></property> <property name="username"><value>sa</value></property> <property name="password"><value></value></property> </bean>
<bean id="authorsquery" class="jdbc.authorsquery" singleton="false"> <constructor-arg index="0"><ref bean="datasource"/></constructor-arg> </bean> </beans>
4:写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;/** * <p>title: 测试spring中的jdbc</p> * <p>description: </p> * <p>copyright: copyright (c) 2004</p> * <p>company: </p> * @author hanker * @version 1.0 */
public class testauthorsquery extends testcase { applicationcontext ctx=null; protected void setup() throws exception { ctx = new filesystemxmlapplicationcontext("d://work//jpetstore/ rc//jdbc//context-jdbc.xml"); } public void testquery(){ system.out.println("[test1...."); authorsquery qry=(authorsquery)ctx.getbean("authorsquery"); list list = qry.execute(); if(list!=null&&list.size()>0){ for(int i=0;i<list.size();i++){ authors author=(authors)list.get(i); stringbuffer buf=new stringbuffer(); buf.append(author.getlname()) .append("|") .append(author.getfname()) .append("|") .append(author.getphone()) .append("|") .append(author.getaddress()); system.out.println(buf.tostring()); } }
}}
5:编译运行,ok》是不是得到了你想要的结果
white|johnson|408 496-7223|10932 bigge rd.green|marjorie|415 986-7020|309 63rd st. #411carson|cheryl|415 548-7723|589 darwin ln.o'leary|michael|408 286-2428|22 cleveland av. #14straight|dean|415 834-2919|5420 college av.smith|meander|913 843-0462|10 mississippi dr.bennet|abraham|415 658-9932|6223 bateman st.dull|ann|415 836-7128|3410 blonde st.gringlesby|burt|707 938-6445|po box 792locksley|charlene|415 585-4620|18 broadway av.greene|morningstar|615 297-2723|22 graybar house rd.blotchet-halls|reginald|503 745-6402|55 hillsdale bl.
6:下面测试另处一种调jdbc的方法
编写authorsquery2.java文件 package jdbc;
import java.util.list; import org.springframework.context.applicationcontextexception; import org.springframework.dao.dataaccessexception; import org.springframework.jdbc.core.sqlparameter; import org.springframework.jdbc.core.support.jdbcdaosupport;
/** * <p>title: 测试spring中的jdbc</p> * <p>description: </p> * <p>copyright: copyright (c) 2004</p> * <p>company: </p> * @author hanker * @version 1.0 */
public class authorsquery2 extends jdbcdaosupport { public authorsquery2() { } public list query(string sql) throws dataaccessexception { return this.getjdbctemplate().queryforlist(sql); }
}
7:配置,在上面加入下面几行:
<bean id="authorsquery2" class="jdbc.authorsquery2"> <property name="datasource"><ref local="datasource"/></property> </bean>
8:写junit. 在testauthorsquery中加入函数 public void testquery2(){ system.out.println("[test2...."); authorsquery2 qry=(authorsquery2)ctx.getbean("authorsquery2"); list list = qry.query("select * from authors"); if(list!=null&&list.size()>0){ for(int i=0;i<list.size();i++){ map map=(map)list.get(i); stringbuffer buf=new stringbuffer(); buf.append(map.get("au_lname") ) .append("|") .append(map.get("au_fname")) .append("|") .append(map.get("phone")) .append("|") .append(map.get("address")); system.out.println(buf.tostring()); } } }
9:编译运行.以上代码经过测试,能够正常运行,
[email protected]