svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / testPostGis.java @ 3109
History | View | Annotate | Download (6.71 KB)
1 |
/*
|
---|---|
2 |
* Created on 03-mar-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.postgis; |
45 |
|
46 |
import java.io.InputStream; |
47 |
import java.sql.DriverManager; |
48 |
import java.sql.ResultSet; |
49 |
import java.sql.Statement; |
50 |
import java.util.Enumeration; |
51 |
|
52 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
53 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser; |
54 |
|
55 |
|
56 |
/**
|
57 |
* @author FJP
|
58 |
*
|
59 |
* TODO To change the template for this generated type comment go to
|
60 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
61 |
*/
|
62 |
public class testPostGis { |
63 |
|
64 |
public static void main(String[] args) |
65 |
{ |
66 |
/* System.err.println("dburl has the following format:");
|
67 |
System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME");
|
68 |
System.err.println("tablename is 'jdbc_test' by default.");
|
69 |
System.exit(1); */
|
70 |
|
71 |
|
72 |
// Tarda casi 28 segundos en recuperar el tema de provincias!!
|
73 |
String dburl = "jdbc:postgresql://localhost/latin1"; |
74 |
String dbuser = "postgres"; |
75 |
String dbpass = "aquilina"; |
76 |
|
77 |
String dbtable = "provin"; |
78 |
|
79 |
java.sql.Connection conn; |
80 |
try
|
81 |
{ |
82 |
System.out.println("Creating JDBC connection..."); |
83 |
Class.forName("org.postgresql.Driver"); |
84 |
Enumeration enumDrivers = DriverManager.getDrivers(); |
85 |
while (enumDrivers.hasMoreElements())
|
86 |
{ |
87 |
System.out.println("Driver " + enumDrivers.nextElement().toString()); |
88 |
} |
89 |
conn = DriverManager.getConnection(dburl, dbuser, dbpass);
|
90 |
// magic trickery to be pgjdbc 7.2 compatible
|
91 |
// This works due to the late binding of data types in most java VMs. As
|
92 |
// this is more a demo source than a real-world app, we can risk this
|
93 |
// problem.
|
94 |
/* if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
|
95 |
((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
|
96 |
((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
|
97 |
} else {
|
98 |
((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry");
|
99 |
((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
|
100 |
} */
|
101 |
|
102 |
conn.setAutoCommit(false);
|
103 |
|
104 |
/*
|
105 |
* Create a statement and execute a select query.
|
106 |
*/
|
107 |
String strSQL = "select ASBINARY(the_geom) as geom, nom_provin from " + dbtable; |
108 |
// String strSQL = "select ASBINARY(the_geom) as geom from " + dbtable;
|
109 |
// strSQL = "select ASTEXT(the_geom), nom_provin as geom from " + dbtable;
|
110 |
/* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,";
|
111 |
strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias";
|
112 |
strSQL = strSQL + " WHERE TRUE";
|
113 |
*/
|
114 |
// PreparedStatement s = conn.prepareStatement(strSQL);
|
115 |
long t1 = System.currentTimeMillis(); |
116 |
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
117 |
// s.execute("begin");
|
118 |
|
119 |
// Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
120 |
// s.setFetchSize(5);
|
121 |
int fetchSize = 5; |
122 |
s.execute("declare wkb_cursor binary cursor for " + strSQL);
|
123 |
ResultSet r = s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor"); |
124 |
/// ResultSet r = s.executeQuery("fetch forward all in wkb_cursor");
|
125 |
|
126 |
// ResultSet r = s.executeQuery(strSQL);
|
127 |
long t2 = System.currentTimeMillis(); |
128 |
|
129 |
System.out.println("Tiempo de consulta:" + (t2 - t1) + " milisegundos"); |
130 |
t1 = System.currentTimeMillis();
|
131 |
WKBParser parser = new WKBParser();
|
132 |
long id=0; |
133 |
/* for (int i=1; i< 100; i++)
|
134 |
{
|
135 |
r.absolute(i);
|
136 |
System.out.println("Row " + i + ":" + r.getString(2));
|
137 |
}
|
138 |
r.beforeFirst();
|
139 |
for (int i=1; i< 100; i++)
|
140 |
{
|
141 |
r.absolute(i);
|
142 |
System.out.println("Row " + i + ":" + r.getString(2));
|
143 |
} */
|
144 |
|
145 |
while( r.next() )
|
146 |
{ |
147 |
/*
|
148 |
* Retrieve the geometry as an object then cast it to the geometry type.
|
149 |
* Print things out.
|
150 |
*/
|
151 |
// Object obj = r.getObject(2);
|
152 |
byte[] arrayByte = r.getBytes(1); |
153 |
IGeometry gp = parser.parse(arrayByte); |
154 |
String strAux = r.getString(2); |
155 |
// int id = r.getInt(2);
|
156 |
// System.out.println("Fila " + id + ":" + obj.toString());
|
157 |
id++; |
158 |
// Geometry regeom = PGgeometry.geomFromString(obj.toString());
|
159 |
|
160 |
// PGgeometry geom = (PGgeometry)obj;
|
161 |
// int id = r.getInt(2);
|
162 |
System.out.println("Row " + id + ":" + strAux); |
163 |
// System.out.println(geom.toString());
|
164 |
// System.out.println("provin=" + r.getString(2));
|
165 |
if ((id % fetchSize) == 0) |
166 |
{ |
167 |
r = s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor"); |
168 |
} |
169 |
|
170 |
} |
171 |
s.close(); |
172 |
conn.close(); |
173 |
t2 = System.currentTimeMillis();
|
174 |
|
175 |
System.out.println("Tiempo de recorrido:" + (t2 - t1) + " milisegundos. NumRegistros recorridos" + id); |
176 |
|
177 |
} |
178 |
catch( Exception e ) |
179 |
{ |
180 |
e.printStackTrace(); |
181 |
} |
182 |
} |
183 |
} |