KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.longStringColumn
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.DriverManager JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31
32 import org.apache.derby.tools.ij;
33 import org.apache.derby.tools.JDBCDisplayUtil;
34 import java.io.*;
35 import java.sql.PreparedStatement JavaDoc;
36
37 /**
38  * Test of strings longer than 64K. Need to test it using Clob because long varchars don't accept data longer than 32700.
39  */

40
41
42 public class longStringColumn {
43
44     public static PreparedStatement JavaDoc psSet;
45     public static PreparedStatement JavaDoc psGet;
46
47     public static void main(String JavaDoc[] args) {
48
49         System.out.println("Test longStringColumn starting");
50
51         String JavaDoc longText;
52         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("... ");
53
54         try {
55             // use the ij utility to read the property file and
56
// make the initial connection.
57
ij.getPropertyArg(args);
58             Connection JavaDoc conn = ij.startJBMS();
59
60             Statement JavaDoc st2 = conn.createStatement();
61             st2.execute("CREATE TABLE TEST(id bigint, body clob(65K))");
62             psSet = conn.prepareStatement("insert into test values(?,?)");
63             psGet = conn.prepareStatement("select body from test where id=?");
64
65             for (long i = 0; i < 65560; i++) {
66
67                 if (i % 10 == 0)
68                     buff.append(" ");
69                 else
70                     buff.append("x");
71
72                 // Show something is happening
73
if (i % 10000 == 0)
74                     System.out.println("... " + i );
75
76                 // only test after buffer length reaches 65500
77
if (buff.length() > 65525) {
78
79                     System.out.println("i = " + i + ", testing length: " + buff.length());
80
81                     longText = buff.toString();
82                     // set the text
83
setBody(i, longText);
84
85                     // now read the text
86
String JavaDoc res = getBody(i);
87                     if (!res.equals(longText)) {
88                         System.out.println("FAIL -- string fetched is incorrect, length is "
89                             + buff.length() + ", expecting string: " + longText
90                             + ", instead got the following: " + res);
91                         break;
92                     }
93                 }
94             }
95
96             conn.close();
97
98         } catch (SQLException JavaDoc e) {
99             dumpSQLExceptions(e);
100         } catch (Throwable JavaDoc e) {
101             System.out.println("FAIL -- unexpected exception:" + e.toString());
102         }
103
104         System.out.println("Test longStringColumn finished");
105
106     }
107
108     private static void setBody(long key, String JavaDoc body) {
109
110         try {
111             psSet.setLong(1, key);
112             psSet.setString(2, body);
113             psSet.executeUpdate();
114
115         } catch (SQLException JavaDoc ex) {
116             ex.printStackTrace();
117
118             System.out.println("FAIL -- unexpected exception");
119             System.exit(-1);
120         }
121     }
122
123     private static String JavaDoc getBody(long key) {
124
125         String JavaDoc result="NO RESULT";
126
127         try {
128             psGet.setLong(1, key);
129             ResultSet JavaDoc rs = psGet.executeQuery();
130
131             if (rs.next())
132                 result = rs.getString(1);
133
134         } catch (SQLException JavaDoc ex) {
135               ex.printStackTrace();
136         }
137
138         return result;
139     }
140
141     static private void dumpSQLExceptions (SQLException JavaDoc se) {
142         System.out.println("FAIL -- unexpected exception: " + se.toString());
143         while (se != null) {
144             System.out.print("SQLSTATE("+se.getSQLState()+"):");
145             se = se.getNextException();
146         }
147     }
148
149 }
150
Popular Tags