KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > db > DB_Oracle


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.db;
15
16 import java.sql.*;
17 import javax.sql.RowSet JavaDoc;
18
19 import oracle.jdbc.*;
20 import oracle.jdbc.rowset.OracleCachedRowSet;
21
22 /**
23  * Oracle Database Port
24  *
25  * @author Jorg Janke
26  * @version $Id: DB_Oracle.java,v 1.19 2003/09/27 01:22:18 jjanke Exp $
27  */

28 public class DB_Oracle implements CompiereDatabase
29 {
30     /**
31      * Oracle Database
32      */

33     public DB_Oracle()
34     {
35     } // DB_Oracle
36

37     /** Static Driver */
38     private OracleDriver s_driver = new OracleDriver();
39
40     /** Default Port */
41     public static final int DEFAULT_PORT = 1521;
42     /** Default Connection Manager Port */
43     public static final int DEFAULT_CM_PORT = 1630;
44
45     /** Connection String */
46     private String JavaDoc m_connectionURL;
47
48     /**
49      * Get Database Name
50      * @return database short name
51      */

52     public String JavaDoc getName()
53     {
54         return Database.DB_ORACLE;
55     } // getName
56

57     /**
58      * Get Database Description
59      * @return database long name and version
60      */

61     public String JavaDoc getDescription()
62     {
63         return s_driver.toString();
64     } // getDescription
65

66     /**
67      * Get Standard JDBC Port
68      * @return standard port
69      */

70     public int getStandardPort()
71     {
72         return DEFAULT_PORT;
73     } // getStandardPort
74

75     /**
76      * Get Database Driver
77      * @return Driver
78      */

79     public Driver getDriver()
80     {
81         return s_driver;
82     } // getDriver
83

84     /**
85      * Get Database Connection String.
86      * <pre>
87      * Timing:
88      * - CM with source_route not in address_list = 28.5 sec
89      * - CM with source_route in address_list = 58.0 sec
90      * - direct = 4.3-8 sec (no real difference if on other box)
91      * - bequeath = 3.4-8 sec
92      * </pre>
93      * @param connection Connection Descriptor
94      * @return connection String
95      */

96     public String JavaDoc getConnectionURL (CConnection connection)
97     {
98         StringBuffer JavaDoc sb = null;
99         // Server Connections (bequeath)
100
if (connection.isBequeath())
101         {
102             sb = new StringBuffer JavaDoc ("jdbc:oracle:oci8:@");
103             // bug: does not work if there is more than one db instance - use Net8
104
// sb.append(connection.getDbName());
105
}
106         else // thin driver
107
{
108             sb = new StringBuffer JavaDoc ("jdbc:oracle:thin:@");
109             // direct connection
110
if (connection.isViaFirewall())
111             {
112                 // (description=(address_list=
113
// ( (source_route=yes)
114
// (address=(protocol=TCP)(host=cmhost)(port=1630))
115
// (address=(protocol=TCP)(host=dev)(port=1521))
116
// (connect_data=(service_name=dev1.compiere.org)))
117
sb.append("(DESCRIPTION=(ADDRESS_LIST=")
118                     .append("(SOURCE_ROUTE=YES)")
119                     .append("(ADDRESS=(PROTOCOL=TCP)(HOST=").append(connection.getFwHost())
120                         .append(")(PORT=").append(connection.getFwPort()).append("))")
121                     .append("(ADDRESS=(PROTOCOL=TCP)(HOST=").append(connection.getDbHost())
122                         .append(")(PORT=").append(connection.getDbPort()).append(")))")
123                     .append("(CONNECT_DATA=(SERVICE_NAME=").append(connection.getDbName()).append(")))");
124             }
125             else
126             {
127                 // dev:1521:dev1
128
sb.append(connection.getDbHost())
129                     .append(":").append(connection.getDbPort())
130                     .append(":").append(connection.getDbName());
131             }
132         }
133         m_connectionURL = sb.toString();
134         return m_connectionURL;
135     } // getConnectionString
136

137     /**
138      * Supports BLOB
139      * @return true if BLOB is supported
140      */

141     public boolean supportsBLOB()
142     {
143         return true;
144     } // supportsBLOB
145

146     /**
147      * String Representation
148      * @return info
149      */

150     public String JavaDoc toString()
151     {
152         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("DB_Oracle[");
153         sb.append(m_connectionURL)
154             .append("]");
155         return sb.toString();
156     } // toString
157

158     /*************************************************************************/
159
160     /**
161      * Convert an individual Oracle Style statements to target database statement syntax.
162      * @param oraStatement oracle statement
163      * @return converted Statement oracle statement
164      * @throws Exception
165      */

166     public String JavaDoc convertStatement (String JavaDoc oraStatement)
167     {
168         return oraStatement;
169     } // convertStatement
170

171     /*************************************************************************/
172
173     /**
174      * Set the RowID
175      * @param pstmt statement
176      * @param pos position in result set
177      * @param rowID ROWID
178      * @throws SQLException
179      */

180     public void setRowID (PreparedStatement pstmt, int pos, Object JavaDoc rowID) throws SQLException
181     {
182     // ((OraclePreparedStatement)pstmt).setROWID (pos, (ROWID)rowID);
183
pstmt.setString(pos, rowID.toString());
184     } // setRowID
185

186     /**
187      * Get the RowID
188      * @param rs result set
189      * @param pos psoition in result set
190      * @return rowID ROWID
191      * @throws SQLException
192      */

193     public Object JavaDoc getRowID (ResultSet rs, int pos) throws SQLException
194     {
195     // return ((OracleResultSet)rs).getROWID (pos);
196
return rs.getString (pos);
197     } // getRowID
198

199     /**
200      * Get RowSet
201      * @param rs ResultSet
202      * @return RowSet
203      * @throws SQLException
204      */

205     public RowSet JavaDoc getRowSet (ResultSet rs) throws SQLException
206     {
207         OracleCachedRowSet rowSet = new OracleCachedRowSet ();
208         rowSet.populate (rs);
209         return rowSet;
210     } // getRowSet
211

212
213     /*************************************************************************/
214
215     /**
216      * Testing
217      * @param args ignored
218      */

219     public static void main (String JavaDoc[] args)
220     {
221         // Connection option 1
222
try
223         {
224             System.setProperty("oracle.jdbc.Trace", "true");
225             DriverManager.registerDriver(new OracleDriver());
226             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@dev:1521:dev", "compiere", "compiere");
227             System.out.println("Catalog=" + con.getCatalog());
228             DatabaseMetaData md = con.getMetaData();
229             System.out.println("URL=" + md.getURL());
230             System.out.println("User=" + md.getUserName());
231             //
232
System.out.println("Catalog");
233             ResultSet rs = md.getCatalogs();
234             while (rs.next())
235                 System.out.println("- " + rs.getString(1));
236             //
237
System.out.println("Table");
238             rs = md.getTables(null, "COMPIERE", null, new String JavaDoc[] {"TABLE"});
239             while (rs.next())
240                 System.out.println("- User=" + rs.getString(2) + " | Table=" + rs.getString(3)
241                     + " | Type=" + rs.getString(4) + " | " + rs.getString(5));
242             //
243
System.out.println("Column");
244             rs = md.getColumns(null, "COMPIERE", "C_ORDER", null);
245             while (rs.next())
246                 System.out.println("- Tab=" + rs.getString(3) + " | Col=" + rs.getString(4)
247                     + " | Type=" + rs.getString(5) + ", " + rs.getString(6)
248                     + " | Size=" + rs.getString(7) + " | " + rs.getString(8)
249                     + " | Digits=" + rs.getString(9) + " | Radix=" + rs.getString(10)
250                     + " | Null=" + rs.getString(11) + " | Rem=" + rs.getString(12)
251                     + " | Def=" + rs.getString(13) + " | " + rs.getString(14)
252                     + " | " + rs.getString(15) + " | " + rs.getString(16)
253                     + " | Ord=" + rs.getString(17) + " | Null=" + rs.getString(18)
254                     );
255
256             con.close();
257         }
258         catch (SQLException ex)
259         {
260             ex.printStackTrace();
261         }
262     } // main
263
} // DB_Oracle
264
Popular Tags