Oracle Plugin
This commit is contained in:
parent
d186075beb
commit
1ae434014f
|
@ -0,0 +1,174 @@
|
|||
package org.bench4q.agent.plugin.basic.Oracle;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.bench4q.agent.plugin.Constructor;
|
||||
import org.bench4q.agent.plugin.Parameter;
|
||||
import org.bench4q.agent.plugin.Plugin;
|
||||
import org.bench4q.agent.plugin.behavior.Behavior;
|
||||
import org.bench4q.agent.plugin.behavior.BehaviorType;
|
||||
import org.bench4q.agent.utils.Type.SupportTypes;
|
||||
|
||||
@Plugin("Oracle")
|
||||
public class OracleDBPlugin {
|
||||
|
||||
private final String hostName;
|
||||
private final String port;
|
||||
private final String dbName;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final String tableUnderTest = "USERS";
|
||||
|
||||
@Constructor
|
||||
public OracleDBPlugin(
|
||||
@Parameter(value = "hostName", type = SupportTypes.Field) String hostName,
|
||||
@Parameter(value = "port", type = SupportTypes.Field) String port,
|
||||
@Parameter(value = "dbName", type = SupportTypes.Field) String dbName,
|
||||
@Parameter(value = "username", type = SupportTypes.Field) String username,
|
||||
@Parameter(value = "password", type = SupportTypes.Field) String password)
|
||||
{
|
||||
this.hostName = hostName;
|
||||
this.port = port;
|
||||
this.dbName = dbName;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
||||
@Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR)
|
||||
public OracleDBReturn insert(
|
||||
@Parameter(value = "key", type = SupportTypes.Field) String key,
|
||||
@Parameter(value = "value", type = SupportTypes.Field) String value)
|
||||
{
|
||||
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
//connection construct and keep until closed£¬ to obtain more effective performance
|
||||
try {
|
||||
|
||||
Class.forName("oracle.jdbc.driver.OracleDriver");
|
||||
String url = "jdbc:oracle:thin:@" + hostName + ":" + port + ":" + dbName;
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
stmt = conn.createStatement();
|
||||
String sql = "INSERT INTO USERS(KEY1, VALUE1) VALUES('" + key + "','" + value + "')";
|
||||
stmt.execute(sql);
|
||||
return new OracleDBReturn(true);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return new OracleDBReturn(false);
|
||||
} catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
return new OracleDBReturn(false);
|
||||
} finally{
|
||||
try {
|
||||
if(stmt != null){
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
if(conn != null){
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR)
|
||||
public OracleDBReturn query(@Parameter(value = "key", type = SupportTypes.Field) String key){
|
||||
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
ResultSet result = null;
|
||||
try {
|
||||
Class.forName("oracle.jdbc.driver.OracleDriver");
|
||||
String url = "jdbc:oracle:thin:@" + hostName + ":" + port + ":" + dbName;
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
stmt = conn.createStatement();
|
||||
String sql = "SELECT * FROM " + tableUnderTest + " WHERE KEY1 = '" + key + "'";
|
||||
result = stmt.executeQuery(sql);
|
||||
while(result.next()){
|
||||
System.out.println("Users info : key1 = " + result.getString(1) + " value1 = " + result.getString(2));
|
||||
}
|
||||
return new OracleDBReturn(true);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return new OracleDBReturn(false);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return new OracleDBReturn(false);
|
||||
} finally{
|
||||
try {
|
||||
if(result != null){
|
||||
result.close();
|
||||
}
|
||||
if(stmt != null){
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
if(conn != null){
|
||||
conn.close();
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Behavior(value = "QueryAll", type = BehaviorType.USER_BEHAVIOR)
|
||||
public OracleDBReturn queryAll(){
|
||||
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
ResultSet result = null;
|
||||
try {
|
||||
Class.forName("oracle.jdbc.driver.OracleDriver");
|
||||
String url = "jdbc:oracle:thin:@" + hostName + ":" + port + ":" + dbName;
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
stmt = conn.createStatement();
|
||||
String sql = "SELECT * FROM " + tableUnderTest;
|
||||
result = stmt.executeQuery(sql);
|
||||
int total = 0;
|
||||
while(result.next()){
|
||||
System.out.println("Users info : key1 = " + result.getString(1) + " value1 = " + result.getString(2));
|
||||
total++;
|
||||
}
|
||||
System.out.println("the total records num : " + total);
|
||||
return new OracleDBReturn(true);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return new OracleDBReturn(false);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return new OracleDBReturn(false);
|
||||
} finally{
|
||||
try {
|
||||
if(result != null){
|
||||
result.close();
|
||||
}
|
||||
if(stmt != null){
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
if(conn != null){
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue