s
C:\Users\Lindows\IdeaProjects\TestGroovy\src\com\iteye\lindows\TestRunnerInsert.groovy
package com.iteye.lindows;
import junit.framework.Assert
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.AfterThread
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Test
import org.junit.runner.RunWith
import java.sql.Connection
import java.sql.DriverManager
import java.sql.Statement
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.assertTrue
/**
* java代码示例,连接数据库进行查询
*
* @author Lindows
*/
@RunWith(GrinderRunner)
class TestRunnerInsert {
public static GTest insertTable
public static Connection conn;
public static Statement stmt; //创建Statement对象
public static List dbLineList; // 准备参数化数据对象
public static fileRownNumber;
@BeforeProcess
public static void beforeProcess() {
insertTable = new GTest(1, "插入表数据")
dbLineList = new File("C:/Users/Lindows/IdeaProjects/TestGroovy/src/resources/database.txt").readLines("UTF-8")
System.out.println("tangxje: " + dbLineList.size())
fileRownNumber = new Random().nextInt(dbLineList.size()-1)+1 //文件database.txt,含titile行0,共10行。
// 方法二,直接获取(文件行数如:10行-1)=9填入下面,nextInt从0~8取值
//fileRownNumber = new Random().nextInt(9)
grinder.logger.info("tangxje ::" + fileRownNumber)
try {
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
grinder.logger.info("成功加载MySQL驱动!");
/*
String url="jdbc:mysql://10.244.73.65:3306/performance_test"; //JDBC的URL
String username = "fabu";
String passwd = "[email protected]";
*/
String url = dbLineList.get(fileRownNumber).split(",")[0];
String username = dbLineList.get(fileRownNumber).split(",")[1];
String passwd = dbLineList.get(fileRownNumber).split(",")[2];
grinder.logger.debug(url)
conn = DriverManager.getConnection(url, username, passwd);
stmt = conn.createStatement(); //创建Statement对象
grinder.logger.info("成功创建stmt!");
} catch (Exception e) {
e.printStackTrace()
}
}
@BeforeThread
public void beforeThread() {
insertTable.record(this, "insertTable")
grinder.statistics.delayReports=true
}
@Test
public void insertTable() {
try{
grinder.logger.info("成功连接到数据库!");
StringBuffer sql = new StringBuffer()
sql.append("insert into tab_001(column_int,column_double,column_decimal,column_varchar_name,column_varchar_address,column_text,column_timestamp_create_time,column_timestamp_update_time) values (1000,300.25,600.98,‘jack‘,‘")
.append("China BeiJing")
.append(new Random().nextInt(99999999))
.append("‘, ‘work in SuNing for 3 years‘,‘2017-06-12 18:00:00‘,‘2017-06-13 15:00:00‘)")
grinder.logger.info(sql.toString())
Thread.sleep(new Random().nextInt(10)) //这里可以设置思考时间10ms
assertTrue(!stmt.execute(sql.toString()))//执行sql insert,!stmt.execute(sql)该写法只于insert true确认
//assertTrue(stmt.execute(sql));//执行sql query , !stmt.execute(sql)该写法只适用于query true确认
}catch(Exception e) {
e.printStackTrace();
}
}
@AfterThread
public void afterThread() {
stmt.close();
conn.close();
}
}
C:\Users\Lindows\IdeaProjects\TestGroovy\src\resources\database.txt
url,username,password
jdbc:mysql://10.244.73.65:3306/performance_test,root,[email protected]
jdbc:mysql://10.244.73.65:3306/performance_test,root,[email protected]
jdbc:mysql://10.244.73.65:3306/performance_test,root,[email protected]
C:\Users\Lindows\Desktop\dev\ptsp_lib\
1 antlr-2.7.7.jar2 asm-3.3.1.jar3 commons-collections-3.2.1.jar4 commons-compress-1.4.1.jar5 commons-io-2.0.1.jar6 commons-lang-2.6.jar7 commons-math3-3.6.1.jar8 dnsjava-2.1.1.jar9 dom4j-1.6.1.jar10 grinder-core-3.9.1.jar11 grinder-dcr-agent-3.9.1.jar12 grinder-http-3.9.1.jar13 grinder-http-patch-3.9.1-patch.jar14 grinder-httpclient-3.9.1.jar15 grinder-httpclient-patch-3.9.1-patch.jar16 grinder-patch-3.9.1-patch.jar17 groovy-all-2.2.1.jar18 gson-2.2.jar19 hamcrest-all-1.1.jar20 hibernate-commons-annotations-4.0.2.Final.jar21 hibernate-core-4.2.2.Final.jar22 java-semver-0.9.0.jar23 javassist-3.15.0-GA.jar24 jboss-transaction-api_1.1_spec-1.0.1.Final.jar25 jcl-over-slf4j-1.6.4.jar26 jcommander-1.32.jar27 jna-3.4.0.jar28 json-20090211.jar29 junit-dep-4.11.jar30 jython-standalone-2.5.3.jar31 log4j-1.2.15.jar32 logback-classic-1.0.0.jar33 logback-core-1.0.0.jar34 ls.txt35 mysql-connector-java-5.1.36.jar36 ngrinder-core-3.4.jar37 ngrinder-groovy-3.4.jar38 ngrinder-runtime-3.4.jar39 pf4j-0.12.0.jar40 picocontainer-2.13.6.jar41 sigar-1.6.4.jar42 sigar-native-1.0.jar43 slf4j-api-1.6.4.jar44 xz-1.0.jar
end
原文:http://www.cnblogs.com/lindows/p/7819852.html