• groovy TestRunnerInsertMysqlSingle


    package com.iteye.lindows.mysql
    
    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.ResultSet
    import java.sql.Statement
    
    import static net.grinder.script.Grinder.grinder
    import static org.junit.Assert.assertTrue
    
    /**
     * java代码示例,连接数据库进行查询
     *
     * @author Lindows
     */
    @RunWith(GrinderRunner)
    class TestRunnerInsertMysqlSingle {
    	public static GTest insertTable
    	public static Connection conn;
    	public static Statement stmt; //创建Statement对象
    
    	@BeforeProcess
    	public static void beforeProcess() {
    		insertTable = new GTest(1, "插入表数据")
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			grinder.logger.info("成功加载MySQL驱动!");
    			String url="jdbc:mysql://10.37.136.162:3306/performance_test";    //JDBC的URL
    			String username = "performance_user";
    			String passwd = "performance!QAZ";
    			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_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','")
    					.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();
    	}
    }
    

  • 相关阅读:
    [Linux]Vmwaer创建CENTOS7虚拟机[转]
    [游记]二访金陵
    [Android]ADB调试: SecurityException: Injecting to another application requires INJECT_EVENTS permission
    [操作系统]记一次未尽的三星 Galaxy A6s(SM-G6200)刷机过程
    [网络]NAT与内网穿透技术初探【待续】
    结构化系统建模之程序流程图|系统流程图|数据流图
    UML系统建模之用例视图
    [Linux]常用命令之【mkdir/touch/cp/rm/ls/mv】
    [Linux]监控外部用户登录及外部主机连接情况
    [Java SE]Unicode解码
  • 原文地址:https://www.cnblogs.com/lindows/p/14390202.html
Copyright © 2020-2023  润新知