tools.c3p0.database.DatabaseAccessImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-autotest-tool Show documentation
Show all versions of java-autotest-tool Show documentation
This is an integration of autotest tools
package tools.c3p0.database;
/**
* Created by zhengyu06 on 2017/9/12
*/
import org.apache.log4j.Logger;
import org.testng.Reporter;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
public class DatabaseAccessImpl implements DatabaseAccessInterface {
private static Logger log = Logger.getLogger(DatabaseAccessFactory.class);
private Connection con;
private Statement stmt = null;
private ResultSet rs = null;
private String preSql = "";
public DatabaseAccessImpl(String dbInfo){
log.trace("DatabaseAccessImpl Constructor was called.");
this.con = DBConnectionManager.getConnection(dbInfo);
}
@Override
public ResultSet executeQueryStmt(String sql){
log.trace(String.format("exec query: %s", sql));
try{
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
log.trace(sql+"\n查询语句执行成功。");
}catch(SQLException e){
log.error(sql+"\n语句执行失败。");
e.printStackTrace();
}
return this.rs;
}
@Override
public int executeUpdateStmt(String sql) throws SQLException {
log.trace("exec stmt...");
int affectedRow = 0;
try{
stmt = con.createStatement();
affectedRow = stmt.executeUpdate(sql);
log.trace(sql+"\n语句执行成功,"+affectedRow+"行数据变动。");
}catch(SQLException e){
log.error(sql+"\n语句执行失败。");
e.printStackTrace();
}
return affectedRow;
}
@Override
public void printRS() throws SQLException{
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();//这个是查询结果一共有多少列。
String colType = "";
String colName = "";
int totalRecordNum = 0;
for(int i=1; i<=colCount; i++){
colName = rsmd.getColumnName(i);
colType = rsmd.getColumnTypeName(i);
System.out.print(colName+"("+colType+") ");
}
while(rs.next()){
Reporter.log("\n");
totalRecordNum++;
for(int i=1; i<=colCount; i++){
colName = rsmd.getColumnName(i);
System.out.print((rs.getObject(colName)==null?"":rs.getObject(colName)).toString()+" ");
}
}
Reporter.log("\n");
Reporter.log("本次结果集共有:"+totalRecordNum+"行,"+colCount+"列。",true);
}
@Override
public String getSpecificVal(String fieldName) throws SQLException{
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();//这个是查询结果一共有多少列。
String value = "";
int totalRecordNum = 0;
while(rs.next()){
Reporter.log("\n");
totalRecordNum++;//行数记录器自加。
if(fieldName!=null&&!"".contentEquals(fieldName)){
// System.out.println("***********");
// String dataTypeName = rs.getObject(fieldName).getClass().getSimpleName();
// if ("Long".contentEquals(dataTypeName)){
// System.out.println("是Long");
// }
value = rs.getObject(fieldName)==null?"":rs.getObject(fieldName).toString();
Reporter.log("你搜索的字段:"+fieldName+" = "+value,true);
}
}
Reporter.log("本次结果集共有:"+totalRecordNum+"行,"+colCount+"列。",true);
Reporter.log("Return给主程序的 "+fieldName+" 字段值为:"+value,true);
return value;
}
@Override
public Map getSpecificVal(String[] fieldGroup) throws SQLException{
ResultSetMetaData rsmd = rs.getMetaData();
Map map = new HashMap<>();
int colCount = rsmd.getColumnCount();//这个是查询结果一共有多少列。
String value = "";
int totalRecordNum = 0;
while(rs.next()){
Reporter.log("\n");
totalRecordNum++;//行数记录器自加。
for (int i=0; i