KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > utilities > sqlc > DBConnection


1 package com.quikj.application.utilities.sqlc;
2
3 import java.sql.*;
4
5 public class DBConnection
6 {
7     public DBConnection(String JavaDoc driver_class) throws Exception JavaDoc
8     {
9         // Load the JDBC driver
10
Class.forName(driver_class).newInstance();
11         instance = this;
12     }
13     
14     public void login(String JavaDoc url,
15     String JavaDoc host,
16     String JavaDoc user,
17     String JavaDoc password,
18     String JavaDoc database) throws SQLException
19     {
20         
21         if (host.equals("localhost") == true)
22         {
23             host = "127.0.0.1";
24         }
25         
26         // Connect to the database
27
String JavaDoc url_str;
28         url_str = new String JavaDoc(url + "://" + host + "/" + database);
29         connection = DriverManager.getConnection(url_str, user, password);
30         
31         this.user = user;
32         this.host = host;
33     }
34     
35     public void login(String JavaDoc url,
36     String JavaDoc host,
37     String JavaDoc user,
38     String JavaDoc password) throws SQLException
39     {
40         login(url, host, user, password, "test");
41     }
42     
43     public static DBConnection Instance()
44     {
45         return instance;
46     }
47     
48     public void dispose() throws SQLException
49     {
50         connection.close();
51         connection = null;
52     }
53     
54     protected void finalize() throws SQLException
55     {
56         if (connection != null)
57         {
58             connection.close();
59         }
60     }
61     
62     public Connection getConnection()
63     {
64         return connection;
65     }
66     
67     public String JavaDoc getUser()
68     {
69         return user;
70     }
71     
72     public String JavaDoc getHost()
73     {
74         return host;
75     }
76     
77     private static DBConnection instance = null;
78     private Connection connection = null;
79     private String JavaDoc user = "";
80     private String JavaDoc host = "";
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Popular Tags