Statistics
| Revision:

root / org.gvsig.jcrs / tags / 2059 / libJCRS / src / es / idr / teledeteccion / connection / EpsgConnection.java @ 72

History | View | Annotate | Download (7.04 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package es.idr.teledeteccion.connection;
42

    
43
import java.io.BufferedReader;
44
import java.io.File;
45
import java.io.IOException;
46
import java.io.InputStreamReader;
47
import java.sql.Connection;
48
import java.sql.ResultSet;
49
import java.sql.SQLException;
50
import java.sql.Statement;
51
import java.util.logging.Logger;
52

    
53
import org.geotools.referencing.factory.epsg.HSQLDataSource;
54
import org.geotools.referencing.factory.iau2000.FactoryUsingHSQL;
55
import org.hsqldb.jdbc.jdbcDataSource;
56

    
57
/**
58
 * Clase para la conexi?n con la base de datos de hsqldb.
59
 * Establece el driver necesario, as? como la cadena de
60
 * conexi?n a la base de datos de la EPSG y la IAU2000
61
 * 
62
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
63
 *
64
 */
65

    
66
public class EpsgConnection extends jdbcDataSource {
67
        
68
        Connection connect;
69

    
70
        public EpsgConnection() {        
71
        /*        try {
72
                        Class.forName("org.hsqldb.jdbcDriver");
73
                } catch (ClassNotFoundException e) {
74
                        e.printStackTrace();
75
                }*/
76
        }
77
        
78
        public void setConnectionEPSG() {
79
                HSQLDataSource ds = new HSQLDataSource();
80
                                
81
                try {
82
                        connect = ds.getConnection();
83
                } catch (SQLException e) {
84
                        // TODO Auto-generated catch block
85
                        e.printStackTrace();
86
                }
87
        }
88
        
89
        /**
90
         * Establece la conexi?n con la base de datos de la IAU2000
91
         *
92
         */
93
        public void setConnectionIAU2000() {
94
                setDatabase("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/iau2000");
95
                setUser("sa");
96
                try {
97
                        connect = super.getConnection();
98
                } catch (SQLException e) {
99
                        // TODO Auto-generated catch block
100
                        e.printStackTrace();
101
                }
102
        }
103
        
104
        /**
105
         * Establece la conexi?n con la base de datos de ESRI
106
         *
107
         */
108
        public void setConnectionEsri() {
109
                setDatabase("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/esri");
110
                setUser("sa");
111
                try {
112
                        connect = super.getConnection();
113
                } catch (SQLException e) {
114
                        // TODO Auto-generated catch block
115
                        e.printStackTrace();
116
                }
117
        }
118
        
119
          
120
        /**
121
     * Sets connection for CRS:* coordinate reference systems
122
     * such as CRS:84
123
     *
124
     */
125
    public void setConnectionNoAuth() {
126
        setDatabase("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/noauth");
127
        setUser("sa");
128
        try {
129
            connect = super.getConnection();
130
        } catch (SQLException e) {
131
            // TODO Auto-generated catch block
132
            e.printStackTrace();
133
        }
134
    }
135
        
136
        /**
137
         * Establece la conexi?n con la base de datos de USR
138
         *
139
         */
140
        public void setConnectionUsr() {
141
                setDatabase("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/usr");
142
                setUser("sa");
143
                try {
144
                        connect = super.getConnection();
145
                } catch (SQLException e) {
146
                        // TODO Auto-generated catch block
147
                        e.printStackTrace();
148
                }
149
        }
150
        
151
        public Connection getConnection(){
152
                return connect;
153
        }
154
        
155
        public void shutdown() throws SQLException {
156

    
157
        Statement st = connect.createStatement();
158

    
159
        // db writes out to files and performs clean shuts down
160
        // otherwise there will be an unclean shutdown
161
        // when program ends
162
        st.execute("SHUTDOWN");
163
        connect.close();    // if there are no other open connection
164
    }
165
        
166
        public synchronized void update(String expression) throws SQLException {
167

    
168
        Statement st = null;
169

    
170
        st = connect.createStatement();    // statements
171

    
172
        int i = st.executeUpdate(expression);    // run the query
173

    
174
        if (i == -1) {
175
            System.out.println("db error : " + expression);
176
        }
177

    
178
        st.close();
179
    }
180
        /*
181
        *//**
182
         * Establece la conexi?n con la base de datos de la EPSG
183
         *
184
         *//*
185
        public void setConnectionEPSG() {
186
                try {                        
187
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_epsg", "sa", "");
188
                } catch (SQLException e1) {
189
                        e1.printStackTrace();
190
                }
191
        }
192
        
193
        *//**
194
         * Establece la conexi?n con la base de datos de la IAU2000
195
         *
196
         *//*
197
        public void setConnectionIAU2000() {
198
                try {                        
199
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_iau2000", "sa", "");                        
200
                } catch (SQLException e1) {
201
                        e1.printStackTrace();
202
                }
203
        }
204
        
205
        *//**
206
         * Establece la conexi?n con la base de datos de ESRI
207
         *
208
         *//*
209
        public void setConnectionEsri() {
210
                try {                        
211
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:/home/jlgomez/gvSIGF2/_fwAndami/gvSIG/extensiones/org.gvsig.crs.extension/db_esri", "sa", "");                        
212
                } catch (SQLException e1) {
213
                        e1.printStackTrace();
214
                }
215
        }
216
        
217
        *//**
218
         * Establece la conexi?n con la base de datos de USR
219
         *
220
         *//*
221
        public void setConnectionUsr() {
222
                try {                        
223
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_usr", "sa", "");                        
224
                } catch (SQLException e1) {
225
                        e1.printStackTrace();
226
                }
227
        }
228
        
229
        public Connection getConnection(){
230
                return connect;
231
        }
232
        
233
        public void shutdown() throws SQLException {
234

235
        Statement st = connect.createStatement();
236

237
        // db writes out to files and performs clean shuts down
238
        // otherwise there will be an unclean shutdown
239
        // when program ends
240
        st.execute("SHUTDOWN");
241
        connect.close();    // if there are no other open connection
242
    }*/
243
        
244
         /**
245
     * Returns {@code true} if the database contains data. This method returns {@code false}
246
     * if an empty EPSG database has been automatically created by HSQL and not yet populated.
247
     */
248
    private static boolean dataExists(final Connection connection) throws SQLException {
249
        final ResultSet tables = connection.getMetaData().getTables(
250
                null, null, "IAU2000_%", new String[] {"TABLE"});
251
        final boolean exists = tables.next();
252
        tables.close();
253
        return exists;
254
    }
255
}