s
C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlMore.groovy
1 packagecom.iteye.lindows.mysql2
3 importjunit.framework.Assert4 importnet.grinder.script.GTest5 importnet.grinder.scriptengine.groovy.junit.GrinderRunner6 importnet.grinder.scriptengine.groovy.junit.annotation.AfterThread7 importnet.grinder.scriptengine.groovy.junit.annotation.BeforeProcess8 importnet.grinder.scriptengine.groovy.junit.annotation.BeforeThread9 importorg.junit.Test10 importorg.junit.runner.RunWith11
12 importjava.sql.Connection13 importjava.sql.DriverManager14 importjava.sql.ResultSet15 importjava.sql.Statement16
17 import staticnet.grinder.script.Grinder.grinder18 import staticorg.junit.Assert.assertTrue19
20 /**
21 * java代码示例,连接数据库进行插入22 *23 *@author15071743(zhangl)24 */
25 @RunWith(GrinderRunner)26 classTestRunnerInsertMysqlMore {27 public staticGTest insertTable28 public staticConnection conn;29 public static Statement stmt; //创建Statement对象
30 public static List databaseIP; //参数化文件多数据库IP
31 public staticfileRowNumber;32 @BeforeProcess33 � //LoggerFactory.getLogger("worker").setLevel(Level.ERROR);//可以关闭该类打印的日志
34 public static voidbeforeProcess() {35 insertTable = new GTest(1, "插入表数据")36 //参数化文件读取--随机数据库IP
37 databaseIP = new File("C:\\Users\\Lindows\\workspace\\GroovyTest\\src\\resources\\databaseIP.txt").readLines()38 fileRowNumber = new Random().nextInt(databaseIP.size()-1)39 }40
41 @BeforeThread42 public voidbeforeThread() {43 insertTable.record(this, "insertTable")44 grinder.statistics.delayReports=true
45 }46
47 @Test48 public voidinsertTable() {49 try{50 //调用Class.forName()方法加载驱动程序
51 Class.forName("com.mysql.jdbc.Driver");52 grinder.logger.info("成功加载MySQL驱动!");53 StringBuffer url = newStringBuffer()54 url.append("jdbc:mysql://")55 .append(databaseIP.get(fileRowNumber).split(",")[0]) //随机获取第一列数据库ip地址
56 .append(":3306/performance_test")57 grinder.logger.info("tangxje_url: " +url.toString());58 String username = "performance_user";59 String passwd = "performance!QAZ";60 conn =DriverManager.getConnection(url.toString(), username, passwd);61 stmt =conn.createStatement();62 grinder.logger.info("成功创建stmt!");63 grinder.logger.info("成功连接到数据库!");64 StringBuffer sql = newStringBuffer()65 grinder.logger.info("tangxje_tab:" + databaseIP.get(fileRowNumber).split(",")[1]);66 sql.append("insert into ")67 .append(databaseIP.get(fileRowNumber).split(",")[1]) //随机获取第二列数据库表名,同行取值列1
68 .append(" (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','")69 .append("China BeiJing")70 .append(new Random().nextInt(99999999))71 .append("', 'work in lindows for 3 years','2017-06-12 18:00:00','2017-06-13 15:00:00')")72 grinder.logger.info("tangxje_sql:" +sql.toString())73 assertTrue(!stmt.execute(sql.toString()))//执行sql insert,!stmt.execute(sql)该写法只于insert true确认74 //assertTrue(stmt.execute(sql));//执行sql query , !stmt.execute(sql)该写法只适用于query true确认
75 }catch(Exception e) {76 e.printStackTrace();77 }78 }79
80 @AfterThread81 public voidafterThread() {82 stmt.close();83 conn.close();84 }85 }
文件:C:\Users\Lindows\workspace\GroovyTest\src\resources\databaseIP.txt
内容:
10.37.153.1,tab_102
10.37.136.165,tab_002
10.37.136.162,tab_002
10.37.136.163,tab_102
C:\Users\Lindows\Desktop\lab\groovy\libs
1 asm-3.3.1.jar
2 commons-lang-2.6.jar
3 commons-lang3-3.3.2.jar
4 commons-logging-1.0.4.jar
5 grinder-core-3.9.1.jar
6 grinder-dcr-agent-3.9.1.jar
7 grinder-http-3.9.1.jar
8 grinder-http-patch-3.9.1-patch.jar
9 grinder-httpclient-3.9.1.jar
10 grinder-httpclient-patch-3.9.1-patch.jar
11 grinder-patch-3.9.1-patch.jar
12 hamcrest-all-1.1.jar
13 json-20090211.jar
14 junit-dep-4.11.jar
15 log4j-1.2.15.jar
16 logback-classic-1.0.0.jar
17 logback-core-1.0.0.jar
18 mysql-connector-java-5.1.36 (1).jar
19 ngrinder-core-3.4.jar
20 ngrinder-groovy-3.4.jar
21 ngrinder-runtime-3.4.jar
22 ngrinder-sh-3.4.jar
23 slf4j-api-1.6.4.jar
end