KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > db > JDatabase


1 /*
2     =========================================================================
3     Package db - Implements the database access module
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /*
30  *
31  * $Id: JDatabase.java,v 1.7 2007/01/28 17:39:19 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.7 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.db;
40
41 import java.sql.Connection JavaDoc;
42 import java.sql.PreparedStatement JavaDoc;
43
44 /** Utility for using the database */
45 public class JDatabase {
46
47   boolean bDriverOK = true;
48   java.sql.Connection JavaDoc conn = null;
49
50
51
52   /**
53    * Constructor. Initializes all class data
54    * and true to connect to a JDBC driver.
55    * @param strDriverName The driver to connect;
56    * @throws java.lang.Exception if the driver could not be loaded (probably ClassNotFoundException)
57   */

58   public JDatabase( java.lang.String JavaDoc strDriverName ) throws java.lang.Exception JavaDoc {
59
60     // Try to load the driver
61
try {
62       Class.forName( strDriverName );
63     } catch( java.lang.Exception JavaDoc e ) {
64       bDriverOK = false;
65       throw e;
66     }
67   }
68
69   /**
70    * Performs all destroy clean in
71    * object.
72    */

73   public void destroy() {
74
75     close();
76     conn = null;
77   }
78
79   /**
80    * Connects to a database server.
81    * @param strConnection Connection string to a server
82    * (Database, path, ...);
83    * @param strUserName Database user name;
84    * @param strPassword User Password;
85    * @return true if successful with no SQLException thrown
86    */

87   public boolean open( java.lang.String JavaDoc strConnection, java.lang.String JavaDoc strUserName, java.lang.String JavaDoc strPassword ) {
88
89     try {
90       conn = java.sql.DriverManager.getConnection( strConnection, strUserName, strPassword );
91     } catch( java.sql.SQLException JavaDoc e ) {
92       System.err.println( "JDatabase.open() - SQLException: " + e );
93       return false;
94     }
95
96     return true;
97   }
98
99   /**
100     * Closes the database connection;
101     * @return true if successful (no Exception thrown)
102     */

103   public boolean close() {
104
105     if( bDriverOK ) {
106       try {
107         conn.close();
108       } catch( java.sql.SQLException JavaDoc e ) {
109         System.err.println( "JDatabase.close() - SQLException: " + e );
110         return false;
111       }
112     }
113     
114     return true;
115   }
116
117   /** @return the java.sql.Connection to the database */
118   public Connection JavaDoc getConnection() {
119     return conn;
120   }
121   
122   /**
123    * Executes a database query returning a recordset.
124    * If you are using parameters, don't concatenate SQL; use getConnection().prepareStatement(...) instead
125    * @param strQuery The database query;
126    * @return the JResultSet from the query
127    */

128   public JResultSet execSQL( java.lang.String JavaDoc strQuery ) {
129
130     if( bDriverOK ) {
131       try {
132         java.sql.Statement JavaDoc stmt = conn.createStatement();
133         java.sql.ResultSet JavaDoc rs = stmt.executeQuery( strQuery );
134         JResultSet resultSet = new JResultSet( stmt, rs );
135
136         return resultSet;
137       } catch( java.sql.SQLException JavaDoc e ) {
138         System.err.println( "JDatabase.execSQL() - SQLException: " + e );
139       }
140     }
141     
142     return null;
143   }
144
145   /**
146    * Executes a SQL query that no returns a recordset.
147    * If you are using parameters, don't concatenate SQL; use getConnection().prepareStatement(...) instead
148    * @param strSQL The database query;
149    * @return the number of rows affected, or -1 if unsuccessful
150    */

151   public int execUpdate( java.lang.String JavaDoc strSQL ) {
152    
153     if( bDriverOK ) {
154       try {
155         java.sql.Statement JavaDoc stmt = conn.createStatement();
156         int nRowsAffcted = stmt.executeUpdate( strSQL );
157         
158         return nRowsAffcted;
159       } catch( java.sql.SQLException JavaDoc e ) {
160         System.err.println( "JDatabase.execUpdate() - SQLException: " + e );
161       }
162     }
163
164     return -1;
165   }
166
167   /**
168    * Executes a SQL statement.
169    * @param strSQL strQuery - The database query;
170    * @return true if successful
171    */

172   public boolean execute( String JavaDoc strSQL ) {
173    
174     if( bDriverOK ) {
175       try {
176         java.sql.Statement JavaDoc stmt = conn.createStatement();
177         
178         stmt.execute( strSQL );
179         
180         return true;
181       } catch( java.sql.SQLException JavaDoc e ) {
182         System.err.println( "JDatabase.execute() - SQLException: " + e );
183       }
184     }
185
186     return false;
187   }
188 }
189
190 // JDatabase class
Popular Tags