KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > JDBCTestDisplayUtil


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil
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.util;
23
24 import java.io.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.DriverManager JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.SQLWarning JavaDoc;
34 import java.sql.Statement JavaDoc;
35 import java.sql.PreparedStatement JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.ResultSetMetaData JavaDoc;
38 import java.sql.Types JavaDoc;
39
40 import java.util.Properties JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.Vector JavaDoc;
43
44
45 import org.apache.derby.impl.tools.ij.ijException;
46
47 import org.apache.derby.tools.JDBCDisplayUtil;
48
49 /**
50    Show common format for Network Server and Embedded Exceptions
51 **/

52
53 public class JDBCTestDisplayUtil extends JDBCDisplayUtil {
54
55     /**
56        Show common format for Network Server and Embedded Exceptions
57        @param out PrintStream to write to
58        @param e Throwable to print
59     */

60     
61     static public void ShowCommonSQLException(PrintStream JavaDoc out, Throwable JavaDoc e) {
62         if (e == null) return;
63         
64         if (e instanceof SQLException JavaDoc)
65         {
66             SQLException JavaDoc se = (SQLException JavaDoc)e;
67             if (isDataConversionException(se))
68                 out.println ("Data Conversion SQLException");
69             else if (isResultSetClosedException(se))
70                 out.println("Result Set Closed Exception");
71             else if (isNullSQLStringException(se))
72                 out.println("Null SQL String Exception");
73             else if (isInvalidParameterException(se))
74                     out.println("Invalid Parameter SQL Exception");
75             else if (isValidOnScrollCursorsException(se))
76                 out.println("Method Only Valid On Scroll Cursors SQL Exception");
77             else if (isInvalidMethodReturnException(se))
78                 out.println("Invalid Method Returning a ResultSet or Row Count SQL Exception");
79             else if (isTableDoesNotExistException(se))
80                     out.println("Table Does Not Exist SQL Exception");
81             else if (isReturnsInvalidResultSetException(se))
82                 out.println("Invalid Method Returning ResultSet SQL Exception");
83             else
84                 ShowSQLException(out, se);
85         }
86         else
87             ShowException(out, e);
88     }
89     
90     static private boolean isDataConversionException(SQLException JavaDoc se)
91     {
92         if ((se.getMessage() != null &&
93              se.getMessage().indexOf("Invalid data conversion") >= 0)
94             || (se.getSQLState() != null &&
95                 (se.getSQLState().equals("22018")
96                  || se.getSQLState().equals("22005")
97                  || se.getSQLState().equals("22007"))))
98             return true;
99         return false;
100     }
101     
102     static private boolean isResultSetClosedException(SQLException JavaDoc se)
103     {
104         if ((se.getMessage() != null &&
105              se.getMessage().indexOf("Invalid operation: result set closed") >= 0)
106             || (se.getSQLState() != null &&
107                 (se.getSQLState().equals("XCL16"))))
108             return true;
109         return false;
110     }
111     
112     static private boolean isNullSQLStringException(SQLException JavaDoc se)
113     {
114         if ((se.getMessage() != null &&
115              se.getMessage().indexOf("Null SQL string passed.") >= 0)
116             || (se.getSQLState() != null &&
117                 (se.getSQLState().equals("XJ067"))))
118             return true;
119         return false;
120     }
121
122     static private boolean isInvalidParameterException(SQLException JavaDoc se)
123     {
124         if ((se.getMessage() != null &&
125              se.getMessage().indexOf("Invalid parameter value") >= 0)
126             || (se.getMessage().indexOf("Invalid fetch size") >= 0)
127             || (se.getMessage().indexOf("Invalid fetch direction") >= 0)
128             || (se.getSQLState() != null &&
129                 (se.getSQLState().equals("XJ066"))))
130             return true;
131         return false;
132     }
133     
134     static private boolean isValidOnScrollCursorsException(SQLException JavaDoc se)
135     {
136         if ((se.getMessage() != null &&
137              se.getMessage().indexOf("' method is only allowed on scroll cursors.") >= 0)
138             || (se.getSQLState() != null &&
139                 (se.getSQLState().equals("XJ061"))))
140             return true;
141         return false;
142     }
143     
144     static private boolean isInvalidMethodReturnException(SQLException JavaDoc se)
145     {
146         if (((se.getMessage() != null &&
147               se.getMessage().indexOf("executeQuery method cannot be used for update.") >= 0)
148              || se.getMessage().indexOf("executeUpdate method cannot be used for query.") >= 0)
149             || (se.getSQLState() != null &&
150                 (se.getSQLState().equals("X0Y78")
151                  || se.getSQLState().equals("X0Y79"))))
152             return true;
153         return false;
154     }
155     
156     static private boolean isTableDoesNotExistException(SQLException JavaDoc se)
157     {
158         if (se.getSQLState() != null &&
159             se.getSQLState().equals("42X05"))
160             return true;
161         return false;
162     }
163     
164     static private boolean isReturnsInvalidResultSetException(SQLException JavaDoc se)
165     {
166         if ((se.getMessage() != null &&
167              se.getMessage().indexOf("cannot be called with a statement that returns a ResultSet.") >= 0)
168             || (se.getSQLState() != null &&
169                 (se.getSQLState().equals("X0Y79"))))
170             return true;
171         return false;
172     }
173 }
174
Popular Tags