From 1ae434014f86db7a3fbc6281523bc92fa03151f7 Mon Sep 17 00:00:00 2001 From: rzs840707 Date: Fri, 19 Sep 2014 15:52:31 +0800 Subject: [PATCH] Oracle Plugin --- .../plugin/basic/Oracle/OracleDBPlugin.java | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/Oracle/OracleDBPlugin.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/Oracle/OracleDBPlugin.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/Oracle/OracleDBPlugin.java new file mode 100644 index 00000000..0771c737 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/Oracle/OracleDBPlugin.java @@ -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(); + } + + } + + + } + + +}