mysql提供dataprovider

來源:互聯網
上載者:User

標籤:自己的   direct   close   drive   map   encoding   roi   substring   exception   

import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.*;import java.util.regex.Matcher;public class SQLProvider implements Iterator<Object[]> {    List<Map<String, String>> sqlList = new ArrayList();    int rowNum=0;     //行數    int curRowNo=0;   //當前行數    String sql = null;    String category = "";    /**     * 在TestNG中由@DataProvider(dataProvider = "name")修飾的方法     * 取SQL資料時,調用此類構造方法(此方法會得到列名並將當前行移到下以後)執行後,轉寄到     * TestNG自己的方法中去,然後由它們調用此類實現的hasNext()、next()方法     * 得到一行資料,然後返回給由@Test(dataProvider = "name")修飾的方法,如此     * 反覆到資料讀完為止     * @param tablename     */    public SQLProvider(String tablename, String ... key) {        String ip = null;        String user = null;        String pw = null;        String db = null;        Properties prop = new Properties();        try {            File directory=new File(".");            String path=".src.test.resources.properties.";            String absolutePath=directory.getCanonicalPath()+path.replaceAll("\\.", Matcher.quoteReplacement("\\"))+"conf.properties";            absolutePath = absolutePath.replace("\\",File.separator).replace("/",File.separator);            InputStream in = new FileInputStream(absolutePath);            prop.load(in);            ip = prop.getProperty("SQLProviderIp").trim();            user = prop.getProperty("SQLProviderUser").trim();            pw = prop.getProperty("SQLProviderPw").trim();            db = prop.getProperty("SQLProviderDB").trim();        } catch (IOException e) {            e.printStackTrace();        }        if (key.length > 0){            for (int i = 0 ;i < key.length;i++){                category += "‘"+key[i]+"‘,";            }            category = category.substring(0,category.length()-1);            sql = "select * from "+tablename+" where category in ("+category+")";        }else{            sql = "select * from " + tablename;        }        sqlList = getConnection(sql,ip,user,pw,db);        this.rowNum = sqlList.size();    }    @Override    public boolean hasNext() {        // TODO Auto-generated method stub        if(rowNum==0||curRowNo>=rowNum){            return false;        }else{            return true;        }    }    /**     * 擷取一組參數,即一行資料     */    @Override    public Object[] next() {        // TODO Auto-generated method stub        Map<String,String> s=new TreeMap<String,String>();        s=sqlList.get(curRowNo);        Object[] d=new Object[1];        d[0]=s;        this.curRowNo++;        return d;    }    @Override    public void remove() {        // TODO Auto-generated method stub        throw new UnsupportedOperationException("remove unsupported");    }    public static   List<Map<String,String>> getConnection(String sql,String ip,String user,String pw,String db) {        Connection conn = null;        List<Map<String, String>> list = new ArrayList();        try {            Class.forName("com.mysql.jdbc.Driver");            String url = "jdbc:mysql://" + ip + "/"+db+"?user=" + user                    + "&password=" + pw                    + "&useUnicode=true&characterEncoding=UTF8";            conn = DriverManager.getConnection(url);            Statement stmt = conn.createStatement();            ResultSet result = stmt.executeQuery(sql);            while (result.next()) {                Map<String, String> m = new HashMap<String, String>();                for (int i = 2; i <= result.getMetaData().getColumnCount(); i++) {                    String k = result.getMetaData().getColumnName(i);                    String v = result.getString(i);                    m.put(k,v);                }                list.add(m);            }            stmt.close();            conn.close();        } catch (Exception e) {            e.printStackTrace();        }        return list;    }/*    public static void main (String []args) throws IOException {        ConnDB d = new ConnDB();        d.setDb("dataprovider");        d.setDbUrl("localhost");        d.setPwd("shen1986");        d.setUserName("root");        d.ConnDB();        System.out.println(getConnection("select * from zfk",d.getConn()));    }*/}

資料庫設定檔:

#DataProvider SQL Config
#----------------------------------------------------------------------------#
SQLProviderIp=10.253.43.98
SQLProviderUser=root
[email protected]#$1234
SQLProviderDB=dataprovider

 

testng中使用:

  

@DataProvider(name="periodnum")
public Iterator<Object[]> periodnum() throws IOException {
return (Iterator<Object[]>)new SQLProvider("mmd","proidnum3","proidnum6","proidnum12");
}

  

 

mysql提供dataprovider

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.