下载完把mysql-connector-java-5.1.45-bin.jar文件加入到eclipse的Build path中就能使用了!!
导包导错了汇报错的!!package shujuqudong;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class MySQL {
WebDriver driver;
@Test(dataProvider="ssa")
public void test(String searchWord1,String searchWord2,String result) {
driver=new FirefoxDriver();
driver.get("http://www.sogou.com");
driver.findElement(By.id("query")).sendKeys(searchWord1+" "+searchWord2);
driver.findElement(By.id("stb")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Boolean wq=driver.getPageSource().contains(result);
System.out.println(wq);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
driver.close();
}
@DataProvider(name ="ssa")
public static Object[][] words(){
return getTestData("testdata");
}
public static Object [][]getTestData(String tableNmae) {
String driver="com.mysql.jdbc.Driver";//声明数据库驱动
String url="jdbc:mysql://127.0.0.1:3306/gloryroad";//ip地址和数据库名称
String name ="root";
String passWord ="1111";
List list =new ArrayList<>();
try {
Class.forName(driver);//设定驱动程序
Connection conn =DriverManager.getConnection(url,name,passWord);//声明连接数据库的对象
if (!conn.isClosed()) {//判断是否连接成功
System.out.println("数据库连接成功");
}
Statement statement =conn.createStatement(); //
String sql="select * from "+tableNmae;
ResultSet rs =statement.executeQuery(sql);//声明resultSet对象,并存储执行sql的数据集
ResultSetMetaData rsMetaData =rs.getMetaData();//
int cols= rsMetaData.getColumnCount(); //获取列数
while (rs.next()) {
String fields[]=new String [cols];
int col =0;
for(int i =0;i
fields[col]= rs.getString(i+1);
col++;
}
list.add(fields);
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));//返回前三个验证内容是否正确!
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("未能找到Mysql驱动类");
e.printStackTrace();
// TODO: handle exception
}
catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
Object [][] result =new Object[list.size()][];
for (int i = 0; i
result[i]=list.get(i);
}
return result;
}
}