首页 > 代码库 > TestRunnerInsertMysqlSingle.groovy

TestRunnerInsertMysqlSingle.groovy

s

 

C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlSingle.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 Lindows
24  */
25 @RunWith(GrinderRunner)
26 class TestRunnerInsertMysqlSingle {
27     public static GTest insertTable
28     public static Connection conn;
29     public static Statement stmt; //创建Statement对象
30 
31     @BeforeProcess
32     public static void beforeProcess() {
33         insertTable = new GTest(1, "插入表数据")
34         try {
35             Class.forName("com.mysql.jdbc.Driver");
36             grinder.logger.info("成功加载MySQL驱动!");
37             String url="jdbc:mysql://10.37.136.162:3306/performance_test";    //JDBC的URL
38             String username = "performance_user";
39             String passwd = "performance!QAZ";
40             conn = DriverManager.getConnection(url, username, passwd);
41             stmt = conn.createStatement(); //创建Statement对象
42             grinder.logger.info("成功创建stmt!");
43         } catch (Exception e) {
44             e.printStackTrace()
45         }
46     }
47 
48     @BeforeThread
49     public void beforeThread() {
50         insertTable.record(this, "insertTable")
51         grinder.statistics.delayReports=true
52     }
53 
54     @Test
55     public void insertTable() {
56         try{
57             grinder.logger.info("成功连接到数据库!");
58             StringBuffer sql = new StringBuffer()
59             sql.append("insert into tab_002(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‘,‘")
60                     .append("China BeiJing")
61                     .append(new Random().nextInt(99999999))
62                     .append("‘, ‘work in SuNing for 3 years‘,‘2017-06-12 18:00:00‘,‘2017-06-13 15:00:00‘)")
63             grinder.logger.info(sql.toString())
64             Thread.sleep(new Random().nextInt(10)) //这里可以设置思考时间10ms
65             assertTrue(!stmt.execute(sql.toString()))//执行sql insert,!stmt.execute(sql)该写法只于insert true确认
66             //assertTrue(stmt.execute(sql));//执行sql query , !stmt.execute(sql)该写法只适用于query true确认
67         }catch(Exception e) {
68             e.printStackTrace();
69         }
70     }
71 
72     @AfterThread
73     public void afterThread() {
74         stmt.close();
75         conn.close();
76     }
77 }

 

 

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

TestRunnerInsertMysqlSingle.groovy