s
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<String> databaseIP; //参数化文件多数据库IP 31 public static fileRowNumber; 32 @BeforeProcess 33 � //LoggerFactory.getLogger("worker").setLevel(Level.ERROR); //可以关闭该类打印的日志 34 public static void beforeProcess() { 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 @BeforeThread 42 public void beforeThread() { 43 insertTable.record(this, "insertTable") 44 grinder.statistics.delayReports=true 45 } 46 47 @Test 48 public void insertTable() { 49 try{ 50 //调用Class.forName()方法加载驱动程序 51 Class.forName("com.mysql.jdbc.Driver"); 52 grinder.logger.info("成功加载MySQL驱动!"); 53 StringBuffer url = new StringBuffer() 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 = new StringBuffer() 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 @AfterThread 81 public void afterThread() { 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