root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / es / idr / teledeteccion / connection / EpsgConnection.java @ 229
History | View | Annotate | Download (5.21 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 |
*/
|
20 |
package es.idr.teledeteccion.connection; |
21 |
|
22 |
import java.sql.Connection; |
23 |
import java.sql.ResultSet; |
24 |
import java.sql.SQLException; |
25 |
import java.sql.Statement; |
26 |
|
27 |
import org.gvsig.crs.CrsFactory; |
28 |
import org.hsqldb.jdbc.jdbcDataSource; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
import es.idr.teledeteccion.connection.epsg.HSQLDataSource; |
33 |
|
34 |
/**
|
35 |
* Clase para la conexi?n con la base de datos de hsqldb.
|
36 |
* Establece el driver necesario, as? como la cadena de
|
37 |
* conexi?n a la base de datos de la EPSG y la IAU2000
|
38 |
*
|
39 |
* @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
|
40 |
*
|
41 |
*/
|
42 |
public class EpsgConnection extends jdbcDataSource { |
43 |
|
44 |
private static final Logger logger = LoggerFactory.getLogger(EpsgConnection.class); |
45 |
|
46 |
Connection connect = null; |
47 |
String connectionType = null; |
48 |
|
49 |
public EpsgConnection() {
|
50 |
/* try {
|
51 |
Class.forName("org.hsqldb.jdbcDriver");
|
52 |
} catch (ClassNotFoundException e) {
|
53 |
logger.error("Can't create EpsgConnection",e);
|
54 |
}*/
|
55 |
} |
56 |
|
57 |
private void setConnection(Connection connection, String type) { |
58 |
this.connect = connection;
|
59 |
this.connectionType = type;
|
60 |
logger.debug("Created connection to '"+connectionType+"'."); |
61 |
} |
62 |
|
63 |
public void setConnectionEPSG() { |
64 |
HSQLDataSource ds = new HSQLDataSource();
|
65 |
|
66 |
try {
|
67 |
setConnection(ds.getConnection(), "EPSG");
|
68 |
} catch (SQLException e) { |
69 |
logger.warn("Cant get conection from HSQLDataSource.", e);
|
70 |
} |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Establece la conexi?n con la base de datos de la IAU2000
|
75 |
*
|
76 |
*/
|
77 |
public void setConnectionIAU2000() { |
78 |
String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/iau2000"; |
79 |
setDatabase(db); |
80 |
setUser("sa");
|
81 |
try {
|
82 |
setConnection(super.getConnection(),"IAU2000"); |
83 |
} catch (SQLException e) { |
84 |
logger.warn("Can't get conection from database '" + db + "'.", e); |
85 |
} |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Establece la conexi?n con la base de datos de ESRI
|
90 |
*
|
91 |
*/
|
92 |
public void setConnectionEsri() { |
93 |
String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/esri"; |
94 |
setDatabase(db); |
95 |
setUser("sa");
|
96 |
try {
|
97 |
setConnection(super.getConnection(),"Esri"); |
98 |
} catch (SQLException e) { |
99 |
logger.warn("Can't get conection from database '" + db + "'.", e); |
100 |
} |
101 |
} |
102 |
|
103 |
/**
|
104 |
* Sets connection for CRS:* coordinate reference systems
|
105 |
* such as CRS:84
|
106 |
*
|
107 |
*/
|
108 |
public void setConnectionNoAuth() { |
109 |
String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/noauth"; |
110 |
setDatabase(db); |
111 |
setUser("sa");
|
112 |
try {
|
113 |
setConnection(super.getConnection(),"NoAuth"); |
114 |
} catch (SQLException e) { |
115 |
logger.warn("Can't get conection from database '" + db + "'.", e); |
116 |
} |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Establece la conexi?n con la base de datos de USR
|
121 |
*
|
122 |
*/
|
123 |
public void setConnectionUsr() { |
124 |
String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/usr"; |
125 |
setDatabase(db); |
126 |
setUser("sa");
|
127 |
try {
|
128 |
setConnection(super.getConnection(),"Usr"); |
129 |
} catch (SQLException e) { |
130 |
logger.warn("Can't get conection from database '" + db + "'.", e); |
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 |
logger.debug("Shutdown connection to '"+connectionType+"' database."); |
148 |
} |
149 |
|
150 |
public synchronized void update(String expression) throws SQLException { |
151 |
|
152 |
Statement st = null; |
153 |
st = connect.createStatement(); |
154 |
int i = st.executeUpdate(expression);
|
155 |
if ( i == -1 ) { |
156 |
logger.warn("db("+connectionType+") error in statement: " + expression); |
157 |
} |
158 |
st.close(); |
159 |
} |
160 |
} |