KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > StaticInitializers > DMLInStaticInitializer


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.StaticInitializers.DMLInStaticInitializer
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util.StaticInitializers;
23
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 /** Test DML statement called from within static initializer */
30 public class DMLInStaticInitializer
31 {
32
33     /* This is the method that is invoked from the outer query */
34     public static int getANumber()
35     {
36         return 1;
37     }
38
39     static
40     {
41         /* Execute a DML statement from within the static initializer */
42         doADMLStatement();
43     }
44
45     private static void doADMLStatement()
46     {
47         ResultSet JavaDoc rs = null;
48
49         try
50         {
51             int value;
52
53             /* Connect to the database */
54             Statement JavaDoc s = DriverManager.getConnection(
55                         "jdbc:default:connection").createStatement();
56
57             /* Execute a DML statement. This depends on t1 existing. */
58             rs = s.executeQuery("SELECT s FROM t1");
59
60             if (rs.next())
61             {
62                 System.out.println("Value of t1.s is " + rs.getShort(1));
63             }
64         }
65         catch (SQLException JavaDoc se)
66         {
67             System.out.println("Caught exception " + se);
68             se.printStackTrace(System.out);
69         }
70         finally
71         {
72             try
73             {
74                 if (rs != null)
75                     rs.close();
76             }
77             catch (SQLException JavaDoc se)
78             {
79                 System.out.println("Caught exception " + se);
80                 se.printStackTrace(System.out);
81             }
82         }
83     }
84 }
85
Popular Tags