C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlMore.groovy
1 package com.iteye.lindows.mysql
2
3 import junit.framework.Assert
4 import net.grinder.script.GTest
5 import net.grinder.scriptengine.groovy.junit.GrinderRunner
6 import net.grinder.scriptengine.groovy.junit.annotation.AfterThread
7 import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
8 import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
9 import org.junit.Test
10 import org.junit.runner.RunWith
11
12 import java.sql.Connection
13 import java.sql.DriverManager
14 import java.sql.ResultSet
15 import java.sql.Statement
16
17 import static net.grinder.script.Grinder.grinder
18 import static org.junit.Assert.assertTrue
19
20 /**
21 * java代码示例,连接数据库进行插入
22 *
23 * @author 15071743(zhangl)
24 */
25 @RunWith(GrinderRunner)
26 class TestRunnerInsertMysqlMore {
27 public static GTest insertTable
28 public static Connection conn;
29 public static Statement stmt; //创建Statement对象
30 public static List databaseIP; //参数化文件多数据库IP
31 public static fileRowNumber;
32 @BeforeProcess
33 public static void beforeProcess() {
34 insertTable = new GTest(1, "插入表数据")
35 //参数化文件读取--随机数据库IP
36 databaseIP = new File("C:\\Users\\Lindows\\workspace\\GroovyTest\\src\\resources\\databaseIP.txt").readLines()
37 fileRowNumber = new Random().nextInt(databaseIP.size()-1)
38 }
39
40 @BeforeThread
41 public void beforeThread() {
42 insertTable.record(this, "insertTable")
43 grinder.statistics.delayReports=true
44 }
45
46 @Test
47 public void insertTable() {
48 try{
49 //调用Class.forName()方法加载驱动程序
50 Class.forName("com.mysql.jdbc.Driver");
51 grinder.logger.info("成功加载MySQL驱动!");
52 StringBuffer url = new StringBuffer()
53 url.append("jdbc:mysql://")
54 .append(databaseIP.get(fileRowNumber).split(",")[0]) // 随机获取第一列数据库ip地址
55 .append(":3306/performance_test")
56 grinder.logger.info("tangxje_url: " + url.toString());
57 String username = "performance_user";
58 String passwd = "performance!QAZ";
59 cOnn= DriverManager.getConnection(url.toString(), username, passwd);
60 stmt = conn.createStatement();
61 grinder.logger.info("成功创建stmt!");
62 grinder.logger.info("成功连接到数据库!");
63 StringBuffer sql = new StringBuffer()
64 grinder.logger.info("tangxje_tab:" + databaseIP.get(fileRowNumber).split(",")[1]);
65 sql.append("insert into ")
66 .append(databaseIP.get(fileRowNumber).split(",")[1]) // 随机获取第二列数据库表名,同行取值列1
67 .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‘,‘")
68 .append("China BeiJing")
69 .append(new Random().nextInt(99999999))
70 .append("‘, ‘work in SuNing for 3 years‘,‘2017-06-12 18:00:00‘,‘2017-06-13 15:00:00‘)")
71 grinder.logger.info("tangxje_sql:" + sql.toString())
72 assertTrue(!stmt.execute(sql.toString()))//执行sql insert,!stmt.execute(sql)该写法只于insert true确认
73 //assertTrue(stmt.execute(sql));//执行sql query , !stmt.execute(sql)该写法只适用于query true确认
74 }catch(Exception e) {
75 e.printStackTrace();
76 }
77 }
78
79 @AfterThread
80 public void afterThread() {
81 stmt.close();
82 conn.close();
83 }
84 }
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