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; line number int currowno=0; Current number of rows String sql = null; String category = ""; /** * When fetching SQL data from a method that is decorated by @dataprovider (Dataprovider = "name") in testng, call such a constructor method (this method gets the column name and moves the current row to the next) after execution, forwards to the * TESTN G their own methods, and then by the Hasnext (), Next () method that they call such implementations, get a row of data, and then return to the method decorated by @test (Dataprovider = "name") so that * repeats until the data is read out * @par AM 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; }}/** * Gets a set of parameters, that is, one row of data */@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 Unsupportedoperationex Ception ("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 (). g Etcolumnname (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 ())); }*/}
Database configuration file:
#DataProvider SQL Config
#----------------------------------------------------------------------------#
sqlproviderip=10.253.43.98
Sqlprovideruser=root
[Email protected]#$1234
Sqlproviderdb=dataprovider
Used in testng:
@DataProvider (name= "Periodnum")
Public iterator<object[]> Periodnum () throws IOException {
Return (iterator<object[]>) New SQLProvider ("MMD", "proidnum3", "Proidnum6", "Proidnum12");
}
MySQL offers Dataprovider