KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > admin > model > DBConnection


1 /*
2  * DBConnection.java
3  *
4  * Created on April 6, 2003, 9:48 AM
5  */

6
7 package com.quikj.application.communicator.admin.model;
8
9 import java.sql.*;
10
11 /**
12  *
13  * @author bhm
14  */

15 public class DBConnection
16 {
17     private static DBConnection instance = null;
18     
19     /** Holds value of property jdbcDriver. */
20     private String JavaDoc jdbcDriver;
21     
22     /** Holds value of property url. */
23     private String JavaDoc url;
24     
25     /** Holds value of property user. */
26     private String JavaDoc user;
27     
28     /** Holds value of property password. */
29     private String JavaDoc password;
30     
31     /** Holds value of property errorMessage. */
32     private String JavaDoc errorMessage = "";
33     
34     
35     /** Creates a new instance of DBConnection */
36     public DBConnection()
37     {
38         instance = this;
39     }
40     
41     public static DBConnection getInstance()
42     {
43         return instance;
44     }
45     
46     /** Getter for property jdbcDriver.
47      * @return Value of property jdbcDriver.
48      *
49      */

50     public String JavaDoc getJdbcDriver()
51     {
52         return this.jdbcDriver;
53     }
54     
55     /** Setter for property jdbcDriver.
56      * @param jdbcDriver New value of property jdbcDriver.
57      *
58      */

59     public void setJdbcDriver(String JavaDoc jdbcDriver)
60     {
61         this.jdbcDriver = jdbcDriver;
62     }
63     
64     /** Getter for property url.
65      * @return Value of property url.
66      *
67      */

68     public String JavaDoc getUrl()
69     {
70         return this.url;
71     }
72     
73     /** Setter for property url.
74      * @param url New value of property url.
75      *
76      */

77     public void setUrl(String JavaDoc url)
78     {
79         this.url = url;
80     }
81     
82     /** Getter for property user.
83      * @return Value of property user.
84      *
85      */

86     public String JavaDoc getUser()
87     {
88         return this.user;
89     }
90     
91     /** Setter for property user.
92      * @param user New value of property user.
93      *
94      */

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

104     public String JavaDoc getPassword()
105     {
106         return this.password;
107     }
108     
109     /** Setter for property password.
110      * @param password New value of property password.
111      *
112      */

113     public void setPassword(String JavaDoc password)
114     {
115         this.password = password;
116     }
117     
118     /** Getter for property errorMessage.
119      * @return Value of property errorMessage.
120      *
121      */

122     public String JavaDoc getErrorMessage()
123     {
124         return this.errorMessage;
125     }
126     
127     public Connection getConnection()
128     {
129         Connection connection = null;
130         try
131         {
132             Class.forName(jdbcDriver).newInstance();
133             
134             connection = DriverManager.getConnection(url, user, password);
135             
136             if (connection == null)
137             {
138                 errorMessage = "Connection to the database url "
139                 + url + " (as " + user + ") failed";
140                 
141                 return null;
142             }
143         }
144         catch (Exception JavaDoc ex)
145         {
146             errorMessage = ex.getClass().getName() + ": " + ex.getMessage() + ". URL = " + url;
147             return null;
148         }
149         
150         return connection;
151     }
152         
153 }
154
Popular Tags