uopspop

Untitled

Oct 6th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. class SQLAgentUser$Pool$thread extends SQLAgent implements Runnable {
  4.  
  5.     String sqlStatements;
  6.     ConnPool connPool;
  7.  
  8.     public SQLAgentUser$Pool$thread(String sqlStatements, ConnPool connPool) {
  9.         this.sqlStatements = sqlStatements;
  10.         this.connPool = connPool;
  11.     }
  12.  
  13.     public static void main(String argv[]) {
  14.         ConnPool connPool = new ConnPool();
  15.         connPool.setDriverName("oracle.jdbc.driver.OracleDriver");
  16.         connPool.setJdbcURL("jdbc:oracle:thin:@localhost:1521:xe");
  17.         connPool.setUserName("user08");
  18.         connPool.setPassword("1111");
  19.         try {
  20.             connPool.setConnectionSwitch("ON"); // 主執行緒: 從資料庫取出連線,建立連線池
  21.         } catch (SQLException ex) {
  22.             System.out.println(" ConnPool 連結失敗:" + ex.getMessage());
  23.         }
  24.  
  25.         SQLAgentUser$Pool$thread sr1 = new SQLAgentUser$Pool$thread(
  26.                 "select * from coffees", connPool);
  27.         SQLAgentUser$Pool$thread sr2 = new SQLAgentUser$Pool$thread(
  28.                 "select * from suppliers", connPool);
  29.         SQLAgentUser$Pool$thread sr3 = new SQLAgentUser$Pool$thread(
  30.                 "select * from coffees", connPool);
  31.         SQLAgentUser$Pool$thread sr4 = new SQLAgentUser$Pool$thread(
  32.                 "select * from suppliers", connPool);
  33.  
  34.         Thread t1 = new Thread(sr1, "執行緒1");
  35.         Thread t2 = new Thread(sr2, "執行緒2");
  36.         Thread t3 = new Thread(sr3, "執行緒3");
  37.         Thread t4 = new Thread(sr4, "執行緒4");
  38.         t1.start();
  39.         t2.start();
  40.         t3.start();
  41.         t4.start();
  42.  
  43.         // 主執行緒: 等候所有執行緒結束執行
  44.         try {
  45.             t1.join();
  46.             t2.join();
  47.             t3.join();
  48.             t4.join();
  49.         } catch (InterruptedException e) {
  50.             System.out.println("Main thread Interrupted");
  51.         }
  52.         // 主執行緒: 關閉連線池, 並將連線歸還資料庫
  53.         try {
  54.             connPool.setConnectionSwitch("OFF");
  55.             System.out.println(" [主執行緒: " + Thread.currentThread().getName()
  56.                     + "] 等候所有執行緒結束執行之後, 才關閉連線池");
  57.         } catch (SQLException ex) {
  58.             System.out.println(" ConnPool 連結失敗:" + ex.getMessage());
  59.         }
  60.     }
  61.  
  62.     public void run() {
  63.         setConnPool(connPool); // 通知父類別,將要使用連線池
  64.  
  65.         System.out.println("執行SQL指令: " + sqlStatements + "......");
  66.  
  67.         try {
  68.             // 執行緒:從連線池中取出連線
  69.             setConnectionSwitch("on");
  70.             Connection con = getConnection();
  71.  
  72.             Statement stmt = con.createStatement();
  73.             ResultSet rs = stmt.executeQuery(sqlStatements);
  74.             ResultSetMetaData rsmd = rs.getMetaData();
  75.             int numberOfColumns = rsmd.getColumnCount();
  76.  
  77.             for (int i = 1; i <= numberOfColumns; i++)
  78.                 System.out.print(rsmd.getColumnName(i) + " ");
  79.  
  80.             System.out.println();
  81.             while (rs.next()) {
  82.                 for (int i = 1; i <= numberOfColumns; i++)
  83.                     System.out.print(rs.getObject(i) + " ");
  84.                 System.out.println();
  85.             }
  86.             System.out.println(Thread.currentThread().getName()
  87.                     + ".....執行成功!\n");
  88.  
  89.             rs.close();
  90.             stmt.close();
  91.             // 執行緒:將連線歸還連線池
  92.             setConnectionSwitch("off");
  93.  
  94.         } catch (SQLException ex) {
  95.             System.err.println("SQLException: " + ex.getMessage());
  96.         }
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment