国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

兩種方法測試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]


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澳门| 长治县| 义乌市| 日喀则市| 崇州市| 西林县| 宜黄县| 江山市| 高碑店市| 巢湖市| 聊城市| 临沧市| 连山| 汾西县| 崇礼县| 贵港市| 嵊泗县| 特克斯县| 克什克腾旗| 高密市| 合作市| 津市市| 湟中县| 北宁市| 西城区| 昌乐县| 阜阳市| 临邑县| 临泉县| 阿城市| 阿合奇县| 扶余县| 拜城县| 城固县| 贞丰县| 响水县| 当雄县| 皮山县| 鹤山市| 张家界市| 威宁|