01: /*
02:
03: Derby - Class org.apache.derbyTesting.functionTests.util.StaticInitializers.DMLInStaticInitializer
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to You under the Apache License, Version 2.0
09: (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;
25: import java.sql.Statement;
26: import java.sql.ResultSet;
27: import java.sql.SQLException;
28:
29: /** Test DML statement called from within static initializer */
30: public class DMLInStaticInitializer {
31:
32: /* This is the method that is invoked from the outer query */
33: public static int getANumber() {
34: return 1;
35: }
36:
37: static {
38: /* Execute a DML statement from within the static initializer */
39: doADMLStatement();
40: }
41:
42: private static void doADMLStatement() {
43: ResultSet rs = null;
44:
45: try {
46: int value;
47:
48: /* Connect to the database */
49: Statement s = DriverManager.getConnection(
50: "jdbc:default:connection").createStatement();
51:
52: /* Execute a DML statement. This depends on t1 existing. */
53: rs = s.executeQuery("SELECT s FROM t1");
54:
55: if (rs.next()) {
56: System.out
57: .println("Value of t1.s is " + rs.getShort(1));
58: }
59: } catch (SQLException se) {
60: System.out.println("Caught exception " + se);
61: se.printStackTrace(System.out);
62: } finally {
63: try {
64: if (rs != null)
65: rs.close();
66: } catch (SQLException se) {
67: System.out.println("Caught exception " + se);
68: se.printStackTrace(System.out);
69: }
70: }
71: }
72: }
|