Source Code Cross Referenced for connectionJdbc20.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » functionTests » tests » jdbcapi » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derbyTesting.functionTests.tests.jdbcapi 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.connectionJdbc20
004:
005:           Licensed to the Apache Software Foundation (ASF) under one or more
006:           contributor license agreements.  See the NOTICE file distributed with
007:           this work for additional information regarding copyright ownership.
008:           The ASF licenses this file to You under the Apache License, Version 2.0
009:           (the "License"); you may not use this file except in compliance with
010:           the License.  You may obtain a copy of the License at
011:
012:              http://www.apache.org/licenses/LICENSE-2.0
013:
014:           Unless required by applicable law or agreed to in writing, software
015:           distributed under the License is distributed on an "AS IS" BASIS,
016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:           See the License for the specific language governing permissions and
018:           limitations under the License.
019:
020:         */
021:
022:        package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024:        import java.sql.Connection;
025:        import java.sql.DriverManager;
026:        import java.sql.ResultSetMetaData;
027:        import java.sql.ResultSet;
028:        import java.sql.Statement;
029:        import java.sql.SQLException;
030:        import java.sql.Types;
031:        import java.sql.PreparedStatement;
032:        import java.sql.CallableStatement;
033:        import java.sql.DatabaseMetaData;
034:        import java.io.*;
035:
036:        import org.apache.derby.tools.ij;
037:        import org.apache.derby.tools.JDBCDisplayUtil;
038:        import org.apache.derbyTesting.functionTests.util.TestUtil;
039:
040:        /**
041:         *This Program Test getConnection()/getStatement().
042:         * @author - suresht
043:         */
044:
045:        public class connectionJdbc20 {
046:
047:            static private boolean isDerbyNet = false;
048:
049:            static private String[] testObjects = { "TABLE TAB1" };
050:
051:            public static void main(String[] args) {
052:                Connection conn, connreturn;
053:                Statement stmt, stmtreturn;
054:
055:                System.out.println("Test connection20 starting");
056:                try {
057:                    // use the ij utility to read the property file and
058:                    // make the initial connection.
059:                    ij.getPropertyArg(args);
060:                    conn = ij.startJBMS();
061:                    isDerbyNet = TestUtil.isNetFramework();
062:                    stmt = conn.createStatement();
063:                    // cleanup table, just in case
064:                    TestUtil.cleanUpTest(stmt, testObjects);
065:                    //create a table, insert a row, do a select from the table,
066:                    stmt.execute("create table tab1("
067:                            + "c1 char(100) for bit data,"
068:                            + "c2 varchar(100) for bit data,"
069:                            + "c3 long varchar for bit data," + "c4 char(100),"
070:                            + "c5 varchar(100)," + "c6 long varchar)");
071:
072:                    connreturn = stmt.getConnection();
073:                    if (conn.equals(connreturn))
074:                        System.out.println("Got Same Connection Object");
075:                    else
076:                        System.out.println("Got Different Connection Object");
077:
078:                    // load some data into this table ..
079:                    load_data(connreturn);
080:
081:                    // read the data   of each type with all the possible functions
082:                    ResultSet rs = stmt.executeQuery("select " + "c1," + "c2,"
083:                            + "c3," + "c4," + "c5," + "c6," + "c1 as c1_spare,"
084:                            + "c2 as c2_spare," + "c3 as c3_spare "
085:                            + "from tab1");
086:                    int loop = 0;
087:                    while (loop < 2) {
088:                        while (rs.next()) {
089:                            for (int i = 1; i < 7; i++) {
090:                                get_using_object(rs, i);
091:                                get_using_string(rs, i);
092:
093:                                get_using_ascii_stream(rs, i);
094:
095:                                if (i < 4) // only c1 , c2, c3
096:                                {
097:                                    get_using_binary_stream(rs, i + 6);
098:                                    get_using_bytes(rs, i + 6);
099:                                }
100:                            }
101:                        }
102:                        // get the statment back from the result set
103:                        stmtreturn = rs.getStatement();
104:                        if (stmt.equals(stmtreturn))
105:                            System.out.println("Got Same Statement Object");
106:                        else
107:                            System.out
108:                                    .println("Got Different Statement Object");
109:
110:                        rs.close();
111:                        rs = stmt.executeQuery("select " + "c1," + "c2,"
112:                                + "c3," + "c4," + "c5," + "c6,"
113:                                + "c1 as c1_spare," + "c2 as c2_spare,"
114:                                + "c3 as c3_spare " + "from tab1");
115:                        loop++;
116:                    }
117:
118:                    stmt.close();
119:
120:                    // Try to get the connection object thro database meta data
121:                    DatabaseMetaData dbmeta = conn.getMetaData();
122:
123:                    rs = dbmeta.getTypeInfo();
124:                    while (rs.next()) {
125:                        System.out.println(rs.getString(1));
126:                    }
127:                    // try to get a statemet from a meta data result set
128:                    stmt = rs.getStatement();
129:
130:                    // Try to get the Connection back from a Metadata
131:                    System.out
132:                            .println("Try to Get the connection back from metadata");
133:                    connreturn = dbmeta.getConnection();
134:                    if (conn.equals(connreturn))
135:                        System.out.println("Got Same Connection Object");
136:                    else
137:                        System.out.println("Got Different Connection Object");
138:
139:                    // Try to get the connection thru callable statement
140:                    CallableStatement cs = conn
141:                            .prepareCall("select * from tab1");
142:                    System.out
143:                            .println(" Try to get the connection back from a callable stmt");
144:                    connreturn = cs.getConnection();
145:                    if (conn.equals(connreturn))
146:                        System.out.println("Got Same Connection Object");
147:                    else
148:                        System.out.println("Got Different Connection Object");
149:
150:                    cs.close();
151:                    conn.close();
152:                    TestUtil.cleanUpTest(stmt, testObjects);
153:
154:                } catch (SQLException e) {
155:                    dumpSQLExceptions(e);
156:                    e.printStackTrace();
157:                } catch (Throwable e) {
158:                    System.out.println("FAIL -- unexpected exception: " + e);
159:                    e.printStackTrace();
160:                }
161:
162:                System.out.println("Test getConnection  finished");
163:            }
164:
165:            static private void load_data(Connection conn) throws Exception {
166:                PreparedStatement pstmt = null;
167:
168:                try {
169:                    pstmt = conn
170:                            .prepareStatement("insert into tab1 values(?,?,?,?,?,?)");
171:                    String c1_value = "C1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
172:                    String c2_value = "C2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
173:                    String c3_value = "C3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
174:                    String c4_value = "C4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
175:                    String c5_value = "C5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
176:                    String c6_value = "C6XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
177:
178:                    pstmt.setBytes(1, c1_value.getBytes("US-ASCII"));
179:                    pstmt.setBytes(2, c2_value.getBytes("US-ASCII"));
180:                    pstmt.setBytes(3, c3_value.getBytes("US-ASCII"));
181:                    pstmt.setString(4, c4_value);
182:                    pstmt.setString(5, c5_value);
183:                    pstmt.setString(6, c6_value);
184:                    pstmt.execute();
185:
186:                    // get the connection back thru preapred statement
187:                    System.out
188:                            .println("Try to get connection using preaparedstatement");
189:                    Connection connreturn = pstmt.getConnection();
190:                    if (conn.equals(connreturn))
191:                        System.out.println("Got Same Connection Object");
192:                    else
193:                        System.out.println("Got Different Connection Object");
194:
195:                } catch (SQLException e) {
196:                    dumpSQLExceptions(e);
197:                    e.printStackTrace();
198:                } catch (Throwable e) {
199:                    System.out.println("Fail -- unexpected exception ");
200:                    e.printStackTrace();
201:                } finally {
202:                    pstmt.close();
203:                }
204:            }
205:
206:            static private void dumpSQLExceptions(SQLException se) {
207:                System.out.println("FAIL -- unexpected exception");
208:                while (se != null) {
209:                    System.out.println("SQLSTATE(" + se.getSQLState() + "): "
210:                            + se);
211:                    se = se.getNextException();
212:                }
213:            }
214:
215:            static private void dumpExpectedSQLExceptions(SQLException se) {
216:                System.out.println("PASS -- expected exception");
217:                while (se != null) {
218:                    System.out.println("SQLSTATE(" + se.getSQLState() + "): "
219:                            + se);
220:                    se = se.getNextException();
221:                }
222:            }
223:
224:            static private int printbytearray(byte[] a, int len, int count) {
225:
226:                for (int i = 0; i < len; i++, count++) {
227:
228:                    System.out.print("x" + Integer.toHexString(a[i]));
229:
230:                    if ((i > 0) && ((i % 20) == 0))
231:                        System.out.println("");
232:                }
233:                return count;
234:            }
235:
236:            static void get_using_object(ResultSet rs, int col_no)
237:                    throws Exception {
238:                System.out.println("getObject(" + col_no + ")");
239:                Object cobj = rs.getObject(col_no);
240:                if (cobj instanceof  byte[]) {
241:                    byte[] bytearray = (byte[]) cobj;
242:                    System.out
243:                            .println("  as byte[] length " + bytearray.length);
244:                    printbytearray(bytearray, bytearray.length, 0);
245:                    System.out.println("");
246:                } else {
247:                    System.out.println("  as String");
248:                    System.out.println(cobj.toString());
249:                }
250:            }
251:
252:            static void get_using_bytes(ResultSet rs, int col_no)
253:                    throws Exception {
254:                System.out.println("getBytes(" + col_no + ")");
255:                byte[] bytearray = (byte[]) rs.getBytes(col_no);
256:                printbytearray(bytearray, bytearray.length, 0);
257:                System.out.println("");
258:            }
259:
260:            static void get_using_string(ResultSet rs, int col_no)
261:                    throws Exception {
262:                String s = rs.getString(col_no);
263:                System.out.println("getString(" + col_no + ") length "
264:                        + s.length());
265:                System.out.println(s);
266:            }
267:
268:            static void get_using_ascii_stream(ResultSet rs, int col_no)
269:                    throws Exception {
270:                System.out.println("getAsciiStream(" + col_no + ")");
271:                int no_bytes_read = 0;
272:                InputStream rsbin = rs.getAsciiStream(col_no);
273:                byte[] bytearray = new byte[200];
274:                int count = 0;
275:                while ((no_bytes_read = rsbin.read(bytearray)) != -1) {
276:                    count = printbytearray(bytearray, no_bytes_read, count);
277:                }
278:                System.out.println("");
279:
280:            }
281:
282:            static void get_using_binary_stream(ResultSet rs, int col_no)
283:                    throws Exception {
284:                System.out.println("getBinaryStream(" + col_no + ")");
285:                int no_bytes_read = 0;
286:                InputStream rsbin = rs.getBinaryStream(col_no);
287:                byte[] bytearray = new byte[200];
288:                int count = 0;
289:                while ((no_bytes_read = rsbin.read(bytearray)) != -1) {
290:                    count = printbytearray(bytearray, no_bytes_read, count);
291:                }
292:                System.out.println("");
293:            }
294:
295:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.