KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > badConnection


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.badConnection
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 package org.apache.derbyTesting.functionTests.tests.derbynet;
22
23 import java.sql.*;
24 import java.util.Vector JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.io.File JavaDoc;
27
28 import java.io.BufferedOutputStream JavaDoc;
29 import org.apache.derbyTesting.functionTests.harness.TimedProcess;
30 import org.apache.derbyTesting.functionTests.util.TestUtil;
31
32 /**
33     This tests various bad connection states
34         - non-existant database
35 */

36
37 public class badConnection
38 {
39     
40     private static Properties JavaDoc properties = new java.util.Properties JavaDoc();
41
42     private static String JavaDoc dbNotFoundDB = "notthere";
43     private static String JavaDoc invalidAttrDB = "testbase;upgrade=notValidValue";
44     private static String JavaDoc derbynetDB = "testbase";
45
46
47     private static Connection newConn(String JavaDoc database,Properties JavaDoc properties) throws Exception JavaDoc
48     {
49         Connection conn = null;
50         String JavaDoc databaseURL = TestUtil.getJdbcUrlPrefix() + database;
51         //System.out.println("URL is: " + databaseURL);
52

53         try {
54             conn = DriverManager.getConnection(databaseURL, properties);
55             if (conn == null)
56                 System.out.println("create connection didn't work");
57             else
58                 System.out.println("Connection made\n");
59
60         }
61         catch (SQLException se)
62         {
63             showSQLException(se);
64         }
65
66         return conn;
67     }
68
69     private static void showSQLException(SQLException e)
70     {
71         System.out.println("passed SQLException all the way to client, then thrown by client...");
72         System.out.println("SQLState is: "+e.getSQLState());
73         System.out.println("vendorCode is: "+e.getErrorCode());
74         System.out.println("nextException is: "+e.getNextException());
75         System.out.println("reason is: "+e.getMessage() +"\n\n");
76     }
77
78     public static void main (String JavaDoc args[]) throws Exception JavaDoc
79     {
80         
81         try
82         {
83             TestUtil.loadDriver();
84             System.out.println("No user/password (Client error)");
85             Connection conn1 = newConn(derbynetDB, properties);
86
87             System.out.println("Database not Found (RDBNFNRM)");
88             properties.put ("user", "admin");
89             properties.put ("password", "admin");
90             conn1 = newConn(dbNotFoundDB, properties);
91             if (conn1 != null)
92                 conn1.close();
93
94             System.out.println("Invalid Attribute value (RDBAFLRM)");
95             conn1 = newConn(invalidAttrDB, properties);
96             if (conn1 != null)
97                 conn1.close();
98         
99         }
100         catch (SQLException se)
101         {
102             showSQLException(se);
103         }
104         catch (Exception JavaDoc e)
105         {
106             e.printStackTrace();
107         }
108     }
109
110
111 }
112
Popular Tags