KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > connectionJdbc20


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.connectionJdbc20
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.jdbcapi;
23 import java.sql.Connection JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.ResultSetMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Types JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.CallableStatement JavaDoc;
32 import java.sql.DatabaseMetaData JavaDoc;
33 import java.io.*;
34
35 import org.apache.derby.tools.ij;
36 import org.apache.derby.tools.JDBCDisplayUtil;
37 import org.apache.derbyTesting.functionTests.util.TestUtil;
38
39 /**
40  *This Program Test getConnection()/getStatement().
41  * @author - suresht
42  */

43
44 public class connectionJdbc20{
45    
46     static private boolean isDerbyNet = false;
47
48     static private String JavaDoc[] testObjects = {"TABLE TAB1"};
49
50     public static void main(String JavaDoc[] args) {
51         Connection JavaDoc conn, connreturn;
52         Statement JavaDoc stmt, stmtreturn;
53
54
55         System.out.println("Test connection20 starting");
56         try
57         {
58             // use the ij utility to read the property file and
59
// make the initial connection.
60
ij.getPropertyArg(args);
61              conn = ij.startJBMS();
62              isDerbyNet = TestUtil.isNetFramework();
63              stmt = conn.createStatement();
64             // cleanup table, just in case
65
TestUtil.cleanUpTest(stmt, testObjects);
66             //create a table, insert a row, do a select from the table,
67
stmt.execute("create table tab1("+
68                                            "c1 char(100) for bit data,"+
69                                            "c2 varchar(100) for bit data," +
70                                            "c3 long varchar for bit data,"+
71                                            "c4 char(100),"+
72                                            "c5 varchar(100),"+
73                                            "c6 long varchar)");
74
75             connreturn = stmt.getConnection();
76             if (conn.equals(connreturn))
77                 System.out.println("Got Same Connection Object");
78             else
79                 System.out.println("Got Different Connection Object");
80            
81             // load some data into this table ..
82
load_data(connreturn);
83             
84             // read the data of each type with all the possible functions
85
ResultSet JavaDoc rs = stmt.executeQuery("select " +
86                              "c1," +
87                              "c2," +
88                              "c3," +
89                              "c4," +
90                              "c5," +
91                              "c6," +
92                              "c1 as c1_spare," +
93                              "c2 as c2_spare," +
94                              "c3 as c3_spare " +
95                              "from tab1");
96             int loop = 0;
97             while(loop < 2 )
98             {
99                 while (rs.next())
100                 {
101                     for(int i=1 ; i < 7 ; i++)
102                     {
103                         get_using_object(rs, i);
104                         get_using_string(rs, i);
105             
106             get_using_ascii_stream(rs, i);
107
108                         if(i < 4 ) // only c1 , c2, c3
109
{
110                             get_using_binary_stream(rs, i + 6);
111                             get_using_bytes(rs, i + 6);
112                         }
113                     }
114                 }
115                 // get the statment back from the result set
116
stmtreturn = rs.getStatement();
117                 if (stmt.equals(stmtreturn))
118                     System.out.println("Got Same Statement Object");
119                 else
120                     System.out.println("Got Different Statement Object");
121                 
122                 rs.close();
123         rs = stmt.executeQuery("select " +
124                        "c1," +
125                        "c2," +
126                        "c3," +
127                        "c4," +
128                        "c5," +
129                        "c6," +
130                        "c1 as c1_spare," +
131                        "c2 as c2_spare," +
132                        "c3 as c3_spare " +
133                        "from tab1");
134         loop++;
135             }
136
137         stmt.close();
138
139             // Try to get the connection object thro database meta data
140
DatabaseMetaData JavaDoc dbmeta = conn.getMetaData();
141
142             rs = dbmeta.getTypeInfo();
143             while (rs.next())
144             {
145                System.out.println(rs.getString(1));
146             }
147             // try to get a statemet from a meta data result set
148
stmt = rs.getStatement();
149
150             // Try to get the Connection back from a Metadata
151
System.out.println("Try to Get the connection back from metadata");
152             connreturn = dbmeta.getConnection();
153             if (conn.equals(connreturn))
154                 System.out.println("Got Same Connection Object");
155             else
156                 System.out.println("Got Different Connection Object");
157
158              
159             // Try to get the connection thru callable statement
160
CallableStatement JavaDoc cs = conn.prepareCall("select * from tab1");
161             System.out.println(" Try to get the connection back from a callable stmt");
162             connreturn = cs.getConnection();
163             if (conn.equals(connreturn))
164                 System.out.println("Got Same Connection Object");
165             else
166                 System.out.println("Got Different Connection Object");
167
168             cs.close();
169             conn.close();
170             TestUtil.cleanUpTest(stmt, testObjects);
171
172         }
173         catch (SQLException JavaDoc e) {
174             dumpSQLExceptions(e);
175             e.printStackTrace();
176         }
177         catch (Throwable JavaDoc e) {
178             System.out.println("FAIL -- unexpected exception: "+e);
179             e.printStackTrace();
180         }
181
182         System.out.println("Test getConnection finished");
183     }
184
185
186     static private void load_data(Connection JavaDoc conn) throws Exception JavaDoc{
187         PreparedStatement JavaDoc pstmt = null;
188
189         try{
190             pstmt = conn.prepareStatement(
191                      "insert into tab1 values(?,?,?,?,?,?)");
192             String JavaDoc c1_value="C1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
193             String JavaDoc c2_value="C2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
194             String JavaDoc c3_value="C3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
195             String JavaDoc c4_value="C4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
196             String JavaDoc c5_value="C5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
197             String JavaDoc c6_value="C6XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
198
199             pstmt.setBytes(1, c1_value.getBytes("US-ASCII"));
200             pstmt.setBytes(2, c2_value.getBytes("US-ASCII"));
201             pstmt.setBytes(3, c3_value.getBytes("US-ASCII"));
202             pstmt.setString(4, c4_value);
203             pstmt.setString(5, c5_value);
204             pstmt.setString(6, c6_value);
205             pstmt.execute();
206
207             // get the connection back thru preapred statement
208
System.out.println("Try to get connection using preaparedstatement");
209             Connection JavaDoc connreturn = pstmt.getConnection();
210             if (conn.equals(connreturn))
211                 System.out.println("Got Same Connection Object");
212             else
213                 System.out.println("Got Different Connection Object");
214
215         }catch(SQLException JavaDoc e){
216              dumpSQLExceptions(e);
217              e.printStackTrace();
218         }catch(Throwable JavaDoc e ){
219              System.out.println("Fail -- unexpected exception ");
220              e.printStackTrace();
221         }finally{
222              pstmt.close();
223         }
224     }
225
226     static private void dumpSQLExceptions (SQLException JavaDoc se) {
227         System.out.println("FAIL -- unexpected exception");
228         while (se != null) {
229             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
230             se = se.getNextException();
231         }
232     }
233
234     static private void dumpExpectedSQLExceptions (SQLException JavaDoc se) {
235         System.out.println("PASS -- expected exception");
236         while (se != null) {
237             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
238             se = se.getNextException();
239         }
240     }
241
242     static private int printbytearray(byte [] a , int len, int count){
243
244         for (int i =0 ; i < len; i++, count++) {
245
246             System.out.print("x" + Integer.toHexString(a[i]));
247
248             if ((i > 0) && ((i % 20) == 0))
249                 System.out.println("");
250         }
251         return count;
252     }
253
254     static void get_using_object(ResultSet JavaDoc rs, int col_no) throws Exception JavaDoc{
255         System.out.println("getObject(" + col_no + ")");
256         Object JavaDoc cobj = rs.getObject(col_no);
257         if (cobj instanceof byte[]){
258             byte[] bytearray = (byte[]) cobj;
259             System.out.println(" as byte[] length " + bytearray.length);
260             printbytearray(bytearray, bytearray.length, 0);
261             System.out.println("");
262         }else {
263             System.out.println(" as String");
264             System.out.println(cobj.toString());
265         }
266     }
267
268     static void get_using_bytes(ResultSet JavaDoc rs, int col_no) throws Exception JavaDoc{
269         System.out.println("getBytes(" + col_no + ")");
270        byte[] bytearray = (byte[]) rs.getBytes(col_no);
271        printbytearray(bytearray, bytearray.length, 0);
272         System.out.println("");
273     }
274
275     static void get_using_string(ResultSet JavaDoc rs, int col_no) throws Exception JavaDoc{
276         String JavaDoc s = rs.getString(col_no);
277         System.out.println("getString(" + col_no + ") length " + s.length());
278         System.out.println(s);
279     }
280
281     static void get_using_ascii_stream(ResultSet JavaDoc rs, int col_no) throws Exception JavaDoc{
282         System.out.println("getAsciiStream(" + col_no + ")");
283         int no_bytes_read = 0;
284         InputStream rsbin = rs.getAsciiStream(col_no);
285         byte [] bytearray = new byte[200];
286         int count = 0;
287         while((no_bytes_read=rsbin.read(bytearray)) != -1)
288         {
289             count = printbytearray(bytearray, no_bytes_read, count);
290         }
291         System.out.println("");
292
293     }
294
295     static void get_using_binary_stream(ResultSet JavaDoc rs, int col_no) throws Exception JavaDoc{
296         System.out.println("getBinaryStream(" + col_no + ")");
297         int no_bytes_read = 0;
298         InputStream rsbin = rs.getBinaryStream(col_no);
299         byte [] bytearray = new byte[200];
300         int count = 0;
301         while((no_bytes_read=rsbin.read(bytearray)) != -1)
302         {
303             count = printbytearray(bytearray, no_bytes_read, count);
304         }
305         System.out.println("");
306     }
307
308 }
309
Popular Tags