KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > maxMemPerTab


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.lang.maxMemPerTab
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.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.apache.derby.tools.JDBCDisplayUtil;
31 import org.apache.derby.tools.ij;
32 import org.apache.derbyTesting.functionTests.util.Formatters;
33 import org.apache.derbyTesting.functionTests.util.TestUtil;
34
35 public class maxMemPerTab {
36
37     public static void main(String JavaDoc[] args) {
38         try {
39             ij.getPropertyArg(args);
40             Connection JavaDoc conn = ij.startJBMS();
41             conn.setAutoCommit(false);
42             
43             createTablesAndInsertData(conn);
44             getStatistics(conn);
45         
46             conn.rollback();
47             conn.close();
48         } catch (SQLException JavaDoc e) {
49             TestUtil.dumpSQLExceptions(e);
50         } catch (Throwable JavaDoc e) {
51             System.out.println("FAIL -- unexpected exception:" + e.toString());
52         }
53     }
54
55     private static void createTablesAndInsertData(Connection JavaDoc conn) throws SQLException JavaDoc {
56         
57         System.out.println("Start creating tables and inserting data ...");
58         
59         //create tables
60
PreparedStatement JavaDoc ps = conn.prepareStatement("create table tab1 (c1 int, c2 varchar(20000))");
61         ps.execute();
62         ps = conn.prepareStatement("create table tab2 (c1 int, c2 varchar(20000))");
63         ps.execute();
64         ps = conn.prepareStatement("create table tab3 (c1 int, c2 varchar(2000))");
65         ps.execute();
66         ps = conn.prepareStatement("create table tab4 (c1 int, c2 varchar(2000))");
67         ps.execute();
68         
69         //insert data
70
String JavaDoc largeStringA20000 = new String JavaDoc(Formatters.repeatChar("a",20000));
71         String JavaDoc largeStringA2000 = new String JavaDoc(Formatters.repeatChar("a",2000));
72         String JavaDoc largeStringB20000 = new String JavaDoc(Formatters.repeatChar("b",20000));
73         String JavaDoc largeStringB2000 = new String JavaDoc(Formatters.repeatChar("b",2000));
74         String JavaDoc largeStringC20000 = new String JavaDoc(Formatters.repeatChar("c",20000));
75         String JavaDoc largeStringC2000 = new String JavaDoc(Formatters.repeatChar("c",2000));
76         String JavaDoc largeStringD20000 = new String JavaDoc(Formatters.repeatChar("d",20000));
77         String JavaDoc largeStringD2000 = new String JavaDoc(Formatters.repeatChar("d",2000));
78
79         ps = conn.prepareStatement("insert into tab1 values (?, ?)");
80         ps.setInt(1, 1);
81         ps.setString(2, largeStringA20000);
82         ps.executeUpdate();
83         ps.setInt(1, 2);
84         ps.setString(2, largeStringB20000);
85         ps.executeUpdate();
86         ps.setInt(1, 3);
87         ps.setString(2, largeStringC20000);
88         ps.executeUpdate();
89         ps.close();
90         ps = conn.prepareStatement("insert into tab2 values (?, ?)");
91         ps.setInt(1, 1);
92         ps.setString(2, largeStringA20000);
93         ps.executeUpdate();
94         ps.setInt(1, 2);
95         ps.setString(2, largeStringC20000);
96         ps.executeUpdate();
97         ps.setInt(1, 3);
98         ps.setString(2, largeStringD20000);
99         ps.executeUpdate();
100         ps.close();
101         ps = conn.prepareStatement("insert into tab3 values (?, ?)");
102         ps.setInt(1, 1);
103         ps.setString(2, largeStringA2000);
104         ps.executeUpdate();
105         ps.setInt(1, 2);
106         ps.setString(2, largeStringB2000);
107         ps.executeUpdate();
108         ps.setInt(1, 3);
109         ps.setString(2, largeStringC2000);
110         ps.executeUpdate();
111         ps.close();
112         ps = conn.prepareStatement("insert into tab4 values (?, ?)");
113         ps.setInt(1, 1);
114         ps.setString(2, largeStringA2000);
115         ps.executeUpdate();
116         ps.setInt(1, 2);
117         ps.setString(2, largeStringC2000);
118         ps.executeUpdate();
119         ps.setInt(1, 3);
120         ps.setString(2, largeStringD2000);
121         ps.executeUpdate();
122         ps.close();
123         
124         System.out.println("... done creating tables and inserting data.");
125     }
126     
127     private static void getStatistics(Connection JavaDoc conn) throws SQLException JavaDoc {
128         
129         Statement JavaDoc stmt = conn.createStatement();
130         stmt.execute("call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1)");
131         System.out.println("Called SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1)");
132         
133         JDBCDisplayUtil.setMaxDisplayWidth(2500);
134         
135         //should use nested loop join due to maxMemoryPerTable property setting
136
executeQuery(stmt,conn,"select * from tab1, tab2 where tab1.c2 = tab2.c2");
137         executeQuery(stmt,conn,"values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS()");
138         
139         //should use hash join, maxMemoryPerTable property value is big enough
140
executeQuery(stmt,conn,"select * from tab3, tab4 where tab3.c2 = tab4.c2");
141         executeQuery(stmt,conn,"values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS()");
142         
143         stmt.close();
144     }
145     
146     private static void executeQuery(Statement JavaDoc stmt, Connection JavaDoc conn, String JavaDoc query) throws SQLException JavaDoc{
147         System.out.println("#### Executing \""+ query + "\"");
148         //Display results for select statements
149
ResultSet JavaDoc rs = stmt.executeQuery(query);
150         JDBCDisplayUtil.DisplayResults(System.out,rs,conn);
151         rs.close();
152     }
153 }
154
Popular Tags