1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 import java.sql.*; 24 import java.util.Vector ; 25 26 27 public class userDefMethods 30 { 31 32 public static void deleteFromATable() throws SQLException 34 { 35 Connection con = DriverManager.getConnection("jdbc:default:connection"); 36 PreparedStatement statement = null; 37 String delStr = null; 38 Statement s = con.createStatement(); 39 ResultSet rs = s.executeQuery("SELECT c1 from new org.apache.derby.catalog.TriggerOldTransitionRows() AS EQ"); 40 Vector keys = new Vector (); 41 while(rs.next()){ 42 keys.addElement(new Long (rs.getLong(1))); 43 } 44 rs.close(); 45 46 statement = 47 con.prepareStatement("delete from t1 where c1 = ?"); 48 for(int i = 0; i < keys.size() ; i++){ 49 long key = ((Long )keys.elementAt(i)).longValue(); 50 statement.setLong(1, key); 51 statement.executeUpdate(); 52 } 53 statement.close(); 54 } 55 56 57 public static void deleteFromParent() throws SQLException 58 { 59 Connection con = DriverManager.getConnection("jdbc:default:connection"); 60 String sqlstmt; 61 Statement stmt = con.createStatement(); 62 sqlstmt = "SELECT a FROM new org.apache.derby.catalog.TriggerOldTransitionRows() AS EQ"; 63 ResultSet rs = stmt.executeQuery(sqlstmt); 64 sqlstmt = "delete from parent where a = ? "; 65 PreparedStatement pstmt = con.prepareStatement(sqlstmt); 66 while(rs.next()){ 67 long value = rs.getLong(1); 68 if(value == 1 || value == 3) 69 value = 4; 70 else 71 value = 5; 72 pstmt.setLong(1,value); 73 pstmt.executeUpdate(); 74 } 75 rs.close(); 76 stmt.close(); 77 pstmt.close(); 78 } 79 80 86 public static void derby388() throws SQLException 87 { 88 System.out.println("Running DERBY-388 Test."); 89 Connection conn = DriverManager.getConnection("jdbc:default:connection"); 90 boolean needCommit = !conn.getAutoCommit(); 91 Statement s = conn.createStatement(); 92 93 s.execute("CREATE TABLE D388_T1 (ID INT)"); 95 s.execute("CREATE TABLE D388_T2 (ID_2 INT)"); 96 s.execute( 97 "CREATE TRIGGER D388_TRIG1 AFTER UPDATE OF ID ON D388_T1" + 98 " REFERENCING NEW AS N_ROW FOR EACH ROW MODE DB2SQL" + 99 " UPDATE D388_T2" + 100 " SET ID_2 = " + 101 " CASE WHEN (N_ROW.ID <= 0) THEN N_ROW.ID" + 102 " ELSE 6 END " + 103 " WHERE N_ROW.ID < ID_2" 104 ); 105 106 if (needCommit) 107 conn.commit(); 108 109 PreparedStatement ps1 = conn.prepareStatement( 111 "INSERT INTO D388_T1 VALUES (?)"); 112 113 PreparedStatement ps2 = conn.prepareStatement( 115 "INSERT INTO D388_T2(ID_2) VALUES (?)"); 116 117 Statement st = conn.createStatement(); 119 for (int i = 0; i < 20; i++) { 120 121 for (int id = 0; id < 10; id++) { 122 123 ps2.setInt(1, id); 124 ps2.executeUpdate(); 125 ps1.setInt(1, 2*id); 126 ps1.executeUpdate(); 127 128 if (needCommit) 129 conn.commit(); 130 131 } 132 133 st.execute("UPDATE D388_T1 SET ID=5"); 139 if (needCommit) 140 conn.commit(); 141 142 } 143 144 s.execute("DROP TABLE D388_T1"); 146 s.execute("DROP TABLE D388_T2"); 147 148 if (needCommit) 149 conn.commit(); 150 151 st.close(); 152 ps1.close(); 153 ps2.close(); 154 155 System.out.println("DERBY-388 Test Passed."); 156 } 157 158 } 159 | Popular Tags |