Guest User

Untitled

a guest
Dec 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. class Connectiondb {
  4.  
  5. public static Connection conn = null;
  6. public static ResultSet rs = null;
  7. public static Statement st = null;
  8. public static PreparedStatement prepStmt = null;
  9. public static ResultSetMetaData rsmd = null;
  10.  
  11. public static int order_id;
  12. public static String function = null;
  13.  
  14. public static void do_tkvasii() throws Exception {
  15. String sqlStmt = "insert into orders_one values (?,?,?,?,?,?,?, '30-MAY-96')";
  16. function = "INSERT";
  17. order_id = get_o_id();
  18. System.out.println(order_id);
  19. conn = getConnection();
  20. conn.setAutoCommit(false);
  21. prepStmt = conn.prepareStatement(sqlStmt);
  22. prepStmt.setInt(1,order_id);
  23. prepStmt.setInt(2,1);
  24. prepStmt.setInt(3,12);
  25. prepStmt.setInt(4,1000);
  26. prepStmt.setInt(5,5);
  27. prepStmt.setInt(6,10);
  28. prepStmt.setInt(7,1);
  29. System.out.println("tkvasii:Going to insert orders_one for :"+order_id);
  30. synchronized(NewThread.class){
  31. prepStmt.executeUpdate();
  32. Thread.sleep(1000);
  33. }
  34. //prepStmt = null;
  35. prepStmt.close();
  36. logoff();
  37.  
  38. }
  39.  
  40. public static int get_o_id() throws Exception{
  41. String str;
  42. if ( function.equals("INSERT") ){
  43. str = "select (max(o_id)+1) from orders_one";}
  44. else{
  45. str = "select max(o_id) from orders_one";}
  46. conn = getConnection();
  47. conn.setAutoCommit(false);
  48. st = conn.createStatement();
  49. synchronized(Connectiondb.class){
  50. rs = st.executeQuery(str);
  51. while (rs.next()){
  52. order_id = rs.getInt(1);
  53. }
  54. }
  55. st = null;
  56. rs = null;
  57. return order_id;
  58. }
  59.  
  60. public static void logoff() throws Exception {
  61. System.out.println("Logoff Thread :"+Thread.currentThread().getName());
  62. conn.close();
  63. Thread.sleep(100);
  64. }
  65. private static Connection getConnection() throws Exception {
  66. String driver = "oracle.jdbc.driver.OracleDriver";
  67. String url = "jdbc:oracle:thin:@adc2190481.us.oracle.com:1521:hadoop";
  68. String username = "tpcc";
  69. String password = "tpcc";
  70. Class.forName(driver);
  71. System.out.println("Logon Thread :"+Thread.currentThread().getName());
  72. return DriverManager.getConnection(url, username, password);
  73. }
  74. }
  75. public class NewThread extends Thread {
  76. NewThread(String str) {
  77. this.start();
  78. }
  79. // This is the entry point for the second thread.
  80. public void run() {
  81. try {
  82.  
  83. System.out.println("Thread: " + Thread.currentThread().getName());
  84. Connectiondb.do_tkvasii();
  85. } catch (Exception e) {
  86. System.out.println("Child interrupted." + e);
  87. }
  88. System.out.println("Exiting child thread.");
  89. }
  90.  
  91. public static void main(String args[]) {
  92. int numberOfThreads=Integer.parseInt(args[0]);
  93. boolean isAlive=true;
  94. NewThread nth[] =new NewThread[numberOfThreads];
  95. System.out.println("passed value:"+args[0]);
  96. for ( int l=0;l<numberOfThreads;l++){
  97. nth[l] = new NewThread(String.valueOf(l));}
  98. while(isAlive)
  99. {
  100. isAlive=false;
  101. for (int i = 0; i < numberOfThreads; i++) {
  102. isAlive|=nth[i].isAlive();
  103. }
  104. try {
  105. Thread.sleep(1000);
  106. } catch (InterruptedException e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }
  110. }
  111.  
  112. System.out.println("Main thread exiting.");
  113. }
  114. }
Add Comment
Please, Sign In to add comment