KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TriggerDemo


1
2 /**
3  * A demonstation of using triggers in a database application.
4  */

5
6 import java.sql.*;
7 import com.mckoi.database.jdbc.MckoiConnection;
8 import com.mckoi.database.jdbc.TriggerListener;
9
10 public class TriggerDemo {
11
12   public static void main(String JavaDoc[] args) {
13
14     System.out.println();
15
16     // Register the Mckoi JDBC Driver
17
try {
18       Class.forName("com.mckoi.JDBCDriver").newInstance();
19     }
20     catch (Exception JavaDoc e) {
21       System.out.println(
22      "Unable to register the JDBC Driver.\n" +
23      "Make sure the classpath is correct.\n" +
24      "For example on Win32; java -cp ../../mkoidb.jar;. TriggerDemo\n" +
25      "On Unix; java -cp ../../mckoidb.jar:. TriggerDemo");
26       return;
27     }
28
29     // This URL specifies we are creating a local database. The
30
// configuration file for the database is found at './ExampleDB.conf'
31
// The 'create=true' argument means we want to create the database. If
32
// the database already exists, it can not be created.
33
String JavaDoc url = "jdbc:mckoi:local://ExampleDB.conf?create=true";
34
35     // The username/password for the database. This will be the username/
36
// password for the user that has full control over the database.
37
// ( Don't use this demo username/password in your application! )
38
String JavaDoc username = "user";
39     String JavaDoc password = "pass1212";
40
41     // Make a connection with the database. This will create the database
42
// and log into the newly created database.
43
Connection connection;
44     try {
45       connection = DriverManager.getConnection(url, username, password);
46     }
47     catch (SQLException e) {
48       System.out.println(
49      "Unable to create the database.\n" +
50      "The reason: " + e.getMessage());
51       return;
52     }
53
54     // --- Wrap the java.sql.Connection object around the Mckoi connection
55
// extention. ---
56
MckoiConnection mckoi_connection = new MckoiConnection(connection);
57
58     try {
59       // Create a Statement object to execute the queries on,
60
Statement statement = connection.createStatement();
61       ResultSet result;
62
63       System.out.println("-- Creating Tables --");
64
65       // Create two tables to test triggers,
66
statement.executeQuery(
67       " CREATE TABLE TriggerTable1 ( " +
68       " trig_column INTEGER NOT NULL ) ");
69
70       statement.executeQuery(
71       " CREATE TABLE TriggerTable2 ( " +
72       " trig_column INTEGER NOT NULL ) ");
73
74       System.out.println("-- Setting Triggers --");
75
76       // Set the triggers on the database,
77
statement.executeQuery("CREATE CALLBACK TRIGGER trig_1 INSERT ON TriggerTable1");
78       statement.executeQuery("CREATE CALLBACK TRIGGER trig_2 DELETE ON TriggerTable1");
79       statement.executeQuery("CREATE CALLBACK TRIGGER trig_3 UPDATE ON TriggerTable2");
80
81       // Set up listeners to listen for trigger events on the JDBC client,
82
mckoi_connection.addTriggerListener("trig_1", new TriggerListener() {
83     public void triggerFired(String JavaDoc trigger_name) {
84       System.out.println("Trigger fired: " + trigger_name);
85     }
86       });
87       mckoi_connection.addTriggerListener("trig_2", new TriggerListener() {
88     public void triggerFired(String JavaDoc trigger_name) {
89       System.out.println("Trigger fired: " + trigger_name);
90     }
91       });
92       mckoi_connection.addTriggerListener("trig_3", new TriggerListener() {
93     public void triggerFired(String JavaDoc trigger_name) {
94       System.out.println("Trigger fired: " + trigger_name);
95     }
96       });
97
98       System.out.println("-- Performing queries to cause triggers --");
99
100       // Perform some queries to fire the triggers.
101

102       statement.executeQuery(
103       " INSERT INTO TriggerTable1 ( trig_column ) VALUES " +
104       " ( 10 ), ( 15 ), ( 20 ) ");
105       statement.executeQuery(
106       " INSERT INTO TriggerTable2 ( trig_column ) VALUES " +
107       " ( 10 ), ( 15 ), ( 20 ) ");
108       statement.executeQuery(
109       " DELETE FROM TriggerTable1 WHERE trig_column = 10 ");
110       statement.executeQuery(
111       " DELETE FROM TriggerTable2 WHERE trig_column = 20 ");
112       statement.executeQuery(
113       " INSERT INTO TriggerTable1 ( trig_column ) " +
114       " SELECT trig_column FROM TriggerTable1 ");
115       statement.executeQuery(
116       " INSERT INTO TriggerTable2 ( trig_column ) " +
117       " SELECT trig_column FROM TriggerTable2 ");
118       statement.executeQuery(
119       " UPDATE TriggerTable1 SET trig_column = trig_column * 12.3 ");
120       statement.executeQuery(
121       " UPDATE TriggerTable2 SET trig_column = trig_column * 12.3 ");
122
123       // Wait 2 seconds for the triggers to fire....
124

125       System.out.println("--- Waiting for triggers ---");
126
127       try {
128     Thread.sleep(2000);
129       }
130       catch (InterruptedException JavaDoc e) { /* ignore */ }
131
132       System.out.println("--- Complete ---");
133
134       // Close the statement and the connection.
135
statement.close();
136       connection.close();
137
138     }
139     catch (SQLException e) {
140       System.out.println(
141     "An error occured\n" +
142     "The SQLException message is: " + e.getMessage());
143
144     }
145
146     // Close the the connection.
147
try {
148       connection.close();
149     }
150     catch (SQLException e2) {
151       e2.printStackTrace(System.err);
152     }
153
154   }
155
156 }
157
Popular Tags