Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- class Connectiondb {
- public static Connection conn = null;
- public static ResultSet rs = null;
- public static Statement st = null;
- public static PreparedStatement prepStmt = null;
- public static ResultSetMetaData rsmd = null;
- public static int order_id;
- public static String function = null;
- public static void do_tkvasii() throws Exception {
- String sqlStmt = "insert into orders_one values (?,?,?,?,?,?,?, '30-MAY-96')";
- function = "INSERT";
- order_id = get_o_id();
- System.out.println(order_id);
- conn = getConnection();
- conn.setAutoCommit(false);
- prepStmt = conn.prepareStatement(sqlStmt);
- prepStmt.setInt(1,order_id);
- prepStmt.setInt(2,1);
- prepStmt.setInt(3,12);
- prepStmt.setInt(4,1000);
- prepStmt.setInt(5,5);
- prepStmt.setInt(6,10);
- prepStmt.setInt(7,1);
- System.out.println("tkvasii:Going to insert orders_one for :"+order_id);
- synchronized(NewThread.class){
- prepStmt.executeUpdate();
- Thread.sleep(1000);
- }
- //prepStmt = null;
- prepStmt.close();
- logoff();
- }
- public static int get_o_id() throws Exception{
- String str;
- if ( function.equals("INSERT") ){
- str = "select (max(o_id)+1) from orders_one";}
- else{
- str = "select max(o_id) from orders_one";}
- conn = getConnection();
- conn.setAutoCommit(false);
- st = conn.createStatement();
- synchronized(Connectiondb.class){
- rs = st.executeQuery(str);
- while (rs.next()){
- order_id = rs.getInt(1);
- }
- }
- st = null;
- rs = null;
- return order_id;
- }
- public static void logoff() throws Exception {
- System.out.println("Logoff Thread :"+Thread.currentThread().getName());
- conn.close();
- Thread.sleep(100);
- }
- private static Connection getConnection() throws Exception {
- String driver = "oracle.jdbc.driver.OracleDriver";
- String url = "jdbc:oracle:thin:@adc2190481.us.oracle.com:1521:hadoop";
- String username = "tpcc";
- String password = "tpcc";
- Class.forName(driver);
- System.out.println("Logon Thread :"+Thread.currentThread().getName());
- return DriverManager.getConnection(url, username, password);
- }
- }
- public class NewThread extends Thread {
- NewThread(String str) {
- this.start();
- }
- // This is the entry point for the second thread.
- public void run() {
- try {
- System.out.println("Thread: " + Thread.currentThread().getName());
- Connectiondb.do_tkvasii();
- } catch (Exception e) {
- System.out.println("Child interrupted." + e);
- }
- System.out.println("Exiting child thread.");
- }
- public static void main(String args[]) {
- int numberOfThreads=Integer.parseInt(args[0]);
- boolean isAlive=true;
- NewThread nth[] =new NewThread[numberOfThreads];
- System.out.println("passed value:"+args[0]);
- for ( int l=0;l<numberOfThreads;l++){
- nth[l] = new NewThread(String.valueOf(l));}
- while(isAlive)
- {
- isAlive=false;
- for (int i = 0; i < numberOfThreads; i++) {
- isAlive|=nth[i].isAlive();
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- System.out.println("Main thread exiting.");
- }
- }
Add Comment
Please, Sign In to add comment