Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / oracle / OracleDriver.java @ 5700

History | View | Annotate | Download (4.25 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.hardcode.gdbms.driver.oracle;
42

    
43
import java.sql.Connection;
44
import java.sql.DriverManager;
45
import java.sql.ResultSet;
46
import java.sql.ResultSetMetaData;
47
import java.sql.SQLException;
48
import java.sql.Statement;
49

    
50
import com.hardcode.gdbms.engine.data.db.JDBCSupport;
51
import com.hardcode.gdbms.engine.data.driver.AbstractJDBCDriver;
52

    
53
public class OracleDriver extends AbstractJDBCDriver {
54

    
55
    private static Exception driverException;
56
    
57
    static {
58
        try {
59
            // Class.forName("com.mysql.jdbc.Driver").newInstance();
60
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
61
            
62
        } catch (Exception ex) {
63
            driverException = ex;
64
        }
65
    }
66

    
67
    
68
    /**
69
     * DOCUMENT ME!
70
     *
71
     * @param host DOCUMENT ME!
72
     * @param port DOCUMENT ME!
73
     * @param dbName DOCUMENT ME!
74
     * @param user DOCUMENT ME!
75
     * @param password DOCUMENT ME!
76
     *
77
     * @return DOCUMENT ME!
78
     *
79
     * @throws SQLException
80
     * @throws RuntimeException DOCUMENT ME!
81
     *
82
     * @see com.hardcode.gdbms.engine.data.driver.DBDriver#connect(java.lang.String)
83
     */
84
    public Connection getConnection(String host, int port, String dbName,
85
        String user, String password) throws SQLException {
86
        if (driverException != null) {
87
            throw new RuntimeException(driverException);
88
        }
89

    
90
        // String connectionString = "jdbc:mysql://" + host;
91
        // String connString="jdbc:oracle:thin:@prodHost:1521:ORCL";
92
        String connectionString="jdbc:oracle:thin:@" + host;
93

    
94
        if (port != -1) {
95
            connectionString += (":" + port);
96
        }
97

    
98
        connectionString += (":" + dbName);
99

    
100
//        if (user != null) {
101
//            connectionString += ("?user=" + user + "&password=" + password);
102
//        }
103

    
104
        return DriverManager.getConnection(connectionString, user, password);
105
    }
106

    
107
    /**
108
     * @see com.hardcode.driverManager.Driver#getName()
109
     */
110
    public String getName() {
111
        return "oracle";
112
    }
113

    
114
        public void open(Connection con, String sql) throws SQLException {
115
        jdbcWriter.setCreateTable(false);
116
        jdbcWriter.setWriteAll(false);
117
        Statement st = con.createStatement();
118
        ResultSet rs = st.executeQuery(sql);
119
        ResultSetMetaData metaData = rs.getMetaData();
120
        
121
                String fields = "";
122
                for (int i=0; i<metaData.getColumnCount(); i++)
123
                {
124
                        if (i==0)
125
                                fields =metaData.getColumnName(i+1);
126
                        else
127
                                fields =fields + ", " + metaData.getColumnName(i+1);
128
                }
129
                rs.close();
130
                String sqlAux = sql.replaceFirst(" [*] ", " " + fields + " ");
131
                System.out.println(sqlAux);
132
                
133
        st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
134
                ResultSet.CONCUR_UPDATABLE);
135
        ResultSet res = st.executeQuery(sqlAux);
136
        if (res.getConcurrency() != ResultSet.CONCUR_UPDATABLE)
137
        {
138
                System.err.println("Error: No se puede editar la tabla " + sql);
139
                jdbcWriter = null;
140
        }
141
        else
142
                jdbcWriter.initialize(con, res);                
143

    
144
                
145
        jdbcSupport = JDBCSupport.newJDBCSupport(con, sql);
146

    
147
        }
148

    
149

    
150
}
151

    
152