Statistics
| Revision:

root / org.gvsig.jcrs / libJCRS / src / es / idr / teledeteccion / connection / EpsgConnection.java @ 38

History | View | Annotate | Download (6.58 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
         * Establece la conexi?n con la base de datos de USR
121
         *
122
         */
123
        public void setConnectionUsr() {
124
                setDatabase("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/usr");
125
                setUser("sa");
126
                try {
127
                        connect = super.getConnection();
128
                } catch (SQLException e) {
129
                        // TODO Auto-generated catch block
130
                        e.printStackTrace();
131
                }
132
        }
133
        
134
        public Connection getConnection(){
135
                return connect;
136
        }
137
        
138
        public void shutdown() throws SQLException {
139

    
140
        Statement st = connect.createStatement();
141

    
142
        // db writes out to files and performs clean shuts down
143
        // otherwise there will be an unclean shutdown
144
        // when program ends
145
        st.execute("SHUTDOWN");
146
        connect.close();    // if there are no other open connection
147
    }
148
        
149
        public synchronized void update(String expression) throws SQLException {
150

    
151
        Statement st = null;
152

    
153
        st = connect.createStatement();    // statements
154

    
155
        int i = st.executeUpdate(expression);    // run the query
156

    
157
        if (i == -1) {
158
            System.out.println("db error : " + expression);
159
        }
160

    
161
        st.close();
162
    }
163
        /*
164
        *//**
165
         * Establece la conexi?n con la base de datos de la EPSG
166
         *
167
         *//*
168
        public void setConnectionEPSG() {
169
                try {                        
170
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_epsg", "sa", "");
171
                } catch (SQLException e1) {
172
                        e1.printStackTrace();
173
                }
174
        }
175
        
176
        *//**
177
         * Establece la conexi?n con la base de datos de la IAU2000
178
         *
179
         *//*
180
        public void setConnectionIAU2000() {
181
                try {                        
182
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_iau2000", "sa", "");                        
183
                } catch (SQLException e1) {
184
                        e1.printStackTrace();
185
                }
186
        }
187
        
188
        *//**
189
         * Establece la conexi?n con la base de datos de ESRI
190
         *
191
         *//*
192
        public void setConnectionEsri() {
193
                try {                        
194
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:/home/jlgomez/gvSIGF2/_fwAndami/gvSIG/extensiones/org.gvsig.crs.extension/db_esri", "sa", "");                        
195
                } catch (SQLException e1) {
196
                        e1.printStackTrace();
197
                }
198
        }
199
        
200
        *//**
201
         * Establece la conexi?n con la base de datos de USR
202
         *
203
         *//*
204
        public void setConnectionUsr() {
205
                try {                        
206
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_usr", "sa", "");                        
207
                } catch (SQLException e1) {
208
                        e1.printStackTrace();
209
                }
210
        }
211
        
212
        public Connection getConnection(){
213
                return connect;
214
        }
215
        
216
        public void shutdown() throws SQLException {
217

218
        Statement st = connect.createStatement();
219

220
        // db writes out to files and performs clean shuts down
221
        // otherwise there will be an unclean shutdown
222
        // when program ends
223
        st.execute("SHUTDOWN");
224
        connect.close();    // if there are no other open connection
225
    }*/
226
        
227
         /**
228
     * Returns {@code true} if the database contains data. This method returns {@code false}
229
     * if an empty EPSG database has been automatically created by HSQL and not yet populated.
230
     */
231
    private static boolean dataExists(final Connection connection) throws SQLException {
232
        final ResultSet tables = connection.getMetaData().getTables(
233
                null, null, "IAU2000_%", new String[] {"TABLE"});
234
        final boolean exists = tables.next();
235
        tables.close();
236
        return exists;
237
    }
238
}