KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > web > JDBCConnection


1 /*
2  * JDBCConnection.java
3  *
4  * Created on May 26, 2002, 9:11 AM
5  */

6
7 package com.quikj.server.web;
8
9 import java.sql.*;
10
11 /**
12  *
13  * @author amit
14  */

15 public class JDBCConnection
16 {
17     
18     private String JavaDoc className = null;
19     
20     private String JavaDoc url = null;
21     
22     private String JavaDoc host = null;
23     
24     private String JavaDoc user = null;
25     
26     private String JavaDoc password = null;
27     
28     private static JDBCConnection instance = null;
29     
30     private static String JavaDoc errorMessage = "";
31     
32     /** Creates a new instance of JDBCConnection */
33     public JDBCConnection(String JavaDoc class_name,
34     String JavaDoc url,
35     String JavaDoc host,
36     String JavaDoc user,
37     String JavaDoc password)
38     {
39         setClassName(class_name);
40         setUrl(url);
41         setHost(host);
42         setUser(user);
43         setPassword(password);
44         
45         instance = this;
46     }
47     
48     public static JDBCConnection getInstance()
49     {
50         return instance;
51     }
52     
53     /** Getter for property className.
54      * @return Value of property className.
55      */

56     public java.lang.String JavaDoc getClassName()
57     {
58         return className;
59     }
60     
61     /** Setter for property className.
62      * @param className New value of property className.
63      */

64     public void setClassName(java.lang.String JavaDoc className)
65     {
66         this.className = className;
67     }
68     
69     /** Getter for property host.
70      * @return Value of property host.
71      */

72     public java.lang.String JavaDoc getHost()
73     {
74         return host;
75     }
76     
77     /** Setter for property host.
78      * @param host New value of property host.
79      */

80     public void setHost(java.lang.String JavaDoc host)
81     {
82         this.host = host;
83     }
84     
85     /** Getter for property password.
86      * @return Value of property password.
87      */

88     public java.lang.String JavaDoc getPassword()
89     {
90         return password;
91     }
92     
93     /** Setter for property password.
94      * @param password New value of property password.
95      */

96     public void setPassword(java.lang.String JavaDoc password)
97     {
98         this.password = password;
99     }
100     
101     /** Getter for property url.
102      * @return Value of property url.
103      */

104     public java.lang.String JavaDoc getUrl()
105     {
106         return url;
107     }
108     
109     /** Setter for property url.
110      * @param url New value of property url.
111      */

112     public void setUrl(java.lang.String JavaDoc url)
113     {
114         this.url = url;
115     }
116     
117     /** Getter for property user.
118      * @return Value of property user.
119      */

120     public java.lang.String JavaDoc getUser()
121     {
122         return user;
123     }
124     
125     /** Setter for property user.
126      * @param user New value of property user.
127      */

128     public void setUser(java.lang.String JavaDoc user)
129     {
130         this.user = user;
131     }
132     
133     public Connection getNewConnection(String JavaDoc database)
134     {
135         Connection connection = null;
136         // load the database class
137
try
138         {
139             Class.forName(className).newInstance();
140
141             if (host.equals("localhost") == true)
142             {
143                 host = "127.0.0.1";
144             }
145             
146             String JavaDoc url_str = url + "://" + host + "/" + database;
147             connection = DriverManager.getConnection(url_str, user, password);
148             
149             if (connection == null)
150             {
151                 errorMessage =
152                 "Connection to the database server "
153                 + host + " database " + database + " (" + user + ") failed";
154                 
155                 return null;
156             }
157             
158         }
159         catch (SQLException ex)
160         {
161             // print error message
162
errorMessage =
163             "An SQL error occured while trying to connect to the database server - "
164             + ex.getMessage();
165             
166             return null;
167         }
168         catch (Exception JavaDoc ex)
169         {
170             // print error message
171
errorMessage =
172             "An error ("
173             + ex.getClass().getName()
174             + ") occured while trying to connect to the database server - "
175             + ex.getMessage();
176             return null;
177         }
178         
179         return connection;
180     }
181     
182     /** Getter for property errorMessage.
183      * @return Value of property errorMessage.
184      */

185     public static java.lang.String JavaDoc getErrorMessage()
186     {
187         return errorMessage;
188     }
189     
190 }
191
Popular Tags