KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > Oracle


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.quercus.lib.db;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.LongValue;
35 import com.caucho.quercus.env.StringValueImpl;
36 import com.caucho.util.L10N;
37
38 import java.sql.Connection JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Statement JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * oracle connection class (oracle has NO object oriented API)
47  */

48 public class Oracle extends JdbcConnectionResource {
49   private static final Logger JavaDoc log = Logger.getLogger(Oracle.class.getName());
50   private static final L10N L = new L10N(Oracle.class);
51
52   public Oracle(Env env,
53                 @Optional("localhost") String JavaDoc host,
54                 @Optional String JavaDoc user,
55                 @Optional String JavaDoc password,
56                 @Optional String JavaDoc db,
57                 @Optional("1521") int port,
58                 @Optional String JavaDoc driver,
59                 @Optional String JavaDoc url)
60   {
61     super(env);
62
63     connectInternal(env, host, user, password, db, port, "", 0, driver, url);
64   }
65
66   /**
67    * Connects to the underlying database.
68    */

69   public boolean connectInternal(Env env,
70                                  @Optional("localhost") String JavaDoc host,
71                                  @Optional String JavaDoc userName,
72                                  @Optional String JavaDoc password,
73                                  @Optional String JavaDoc dbname,
74                                  @Optional("5432") int port,
75                                  @Optional String JavaDoc socket,
76                                  @Optional int flags,
77                                  @Optional String JavaDoc driver,
78                                  @Optional String JavaDoc url)
79   {
80     if (isConnected()) {
81       env.warning(L.l("Connection is already opened to '{0}'", this));
82       return false;
83     }
84
85     try {
86
87       if (host == null || host.equals(""))
88         host = "localhost";
89
90       if (driver == null || driver.equals("")) {
91         driver = "oracle.jdbc.OracleDriver";
92       }
93
94       if (url == null || url.equals("")) {
95         if (dbname.indexOf("//") == 0) {
96           // db is the url itself: "//db_host[:port]/database_name"
97
url = "jdbc:oracle:thin:@" + dbname.substring(2);
98           url = url.replace('/', ':');
99         } else {
100           url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbname;
101         }
102       }
103
104       Connection JavaDoc jConn = env.getConnection(driver, url, userName, password);
105
106       setConnection(host, userName, password, dbname, port, jConn, driver, url);
107
108       return true;
109
110     } catch (SQLException JavaDoc e) {
111       env.warning("A link to the server could not be established. " + e.toString());
112       env.setSpecialValue("oracle.connectErrno",new LongValue(e.getErrorCode()));
113       env.setSpecialValue("oracle.connectError", new StringValueImpl(e.getMessage()));
114
115       log.log(Level.FINE, e.toString(), e);
116
117       return false;
118     } catch (Exception JavaDoc e) {
119       env.warning("A link to the server could not be established. " + e.toString());
120       env.setSpecialValue("oracle.connectError", new StringValueImpl(e.getMessage()));
121
122       log.log(Level.FINE, e.toString(), e);
123       return false;
124     }
125   }
126
127   /**
128    * returns a prepared statement
129    */

130   public OracleStatement prepare(Env env, String JavaDoc query)
131   {
132     OracleStatement stmt = new OracleStatement((Oracle) validateConnection());
133
134     stmt.prepare(query);
135
136     return stmt;
137   }
138
139   /**
140    * Creates a database-specific result.
141    */

142   protected JdbcResultResource createResult(Statement JavaDoc stmt,
143                                             ResultSet JavaDoc rs)
144   {
145     return new OracleResult(stmt, rs, this);
146   }
147
148
149   public String JavaDoc toString()
150   {
151     if (isConnected())
152       return "Oracle[" + getHost() + "]";
153     else
154       return "Oracle[]";
155   }
156 }
157
Popular Tags