日期:2014-05-16  浏览次数:20389 次

SC测试总结(一)--数据库测试Demo
做数据库测试时,一般使用jmeterJDBC或者直连数据库执行executeQuery
目前先总结下项目测试时写的部分代码,留个备份
由于项目混合使用nosql和sql数据库,而sql数据库在使用jdbc时,可以共用的连接初始化逻辑。所以先记录下,该部分操作
部分参考网络,以此记录
package baor.test.sql;

public abstract class ConnPropInfo {
	public abstract String getUrl();
	public abstract String getDriver();
	public abstract String getUser();
	public abstract String getPwd();

}

package baor.test.sql;

import java.util.Properties;

public class ConnPropInfoMySQL extends ConnPropInfo {

	private String url;
	private String driver;
	private String pwd;
	private String user;

	// static String defaultDriver = "com.mysql.jdbc.Driver";

	@Override
	public String getUrl() {
		// TODO Auto-generated method stub
		System.out.println(url);
		return url;
	}

	@Override
	public String getDriver() {
		// TODO Auto-generated method stub
		System.out.println(driver);
		return driver;
	}

	@Override
	public String getUser() {
		// TODO Auto-generated method stub
		System.out.println(user);
		return user;
	}

	@Override
	public String getPwd() {
		// TODO Auto-generated method stub
		System.out.println(pwd);
		return pwd;
	}

	public ConnPropInfoMySQL(PropInfoUtil prop) {
		Properties demoProp = prop.LoadPropInfo();
		this.url = demoProp.getProperty("url");
		this.driver = demoProp.getProperty("driver");
		this.pwd = demoProp.getProperty("pwd");
		this.user = demoProp.getProperty("user");
	}

}


package baor.test.sql;

public abstract class PropInfoFactory {
	
	public abstract ConnPropInfo createPropInfo();

}


package baor.test.sql;

public class PropMySQLFactory extends PropInfoFactory {
	PropInfoUtil prop ;
	@Override
	public ConnPropInfo createPropInfo() {
		// TODO Auto-generated method stub
		prop = new PropInfoUtil();
		return new ConnPropInfoMySQL(prop);
	}
}


package baor.test.sql;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class PropInfoUtil {
	public Properties LoadPropInfo() {
		Properties prop = new Properties();
		InputStream input = null;
		try {
			input = new FileInputStream("DB.properties");
			prop.load(input);
			System.out.println(prop.getProperty("driver"));
			System.out.println(prop.getProperty("url"));
			System.out.println(prop.getProperty("user"));
			System.out.println(prop.getProperty("pwd"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return prop;
	}

}


package baor.test.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnUtil {
	Connection connection = null;
	
	public Connection getConnection (ConnPropInfo prop){
		if (connection == null) {
			try {
				// 调用Class.forName()方法加载驱动程序
				Class.forName(prop.getDriver());
				System.out.println("成功加载MySQL驱动!");
				connection = DriverManager.getConnection(prop.getUrl(), prop.getUser(),
						prop.getPwd());
				System.out.print("成功连接到数据库!");
			} catch (ClassNotFoundException e1) {
				System.out.println("找不到MySQL驱动!");
				e1.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return connection;
		
	}
	
	public void closeConnection() {
		try {
			if (connection !=null ) {
				connection.close();
				connection = null;
				System.out.println("连接关闭!!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]) {
		//测试