Statistics
| Revision:

root / branches / FMap_SLD / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / testPostGis.java @ 1755

History | View | Annotate | Download (5.74 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.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/Pruebas";
74
      String dbuser = "postgres";
75
      String dbpass = "aquilina";
76

    
77
      String dbtable = "vias";
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 from " + dbtable;
108
            /* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; 
109
            strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; 
110
            strSQL = strSQL + " WHERE TRUE";
111
            */
112
            // PreparedStatement s = conn.prepareStatement(strSQL);
113
            long t1 = System.currentTimeMillis();
114
            Statement s = conn.createStatement();
115
                        // s.execute("begin");
116
            
117
            // Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
118
            s.setFetchSize(2000);
119
                        s.execute("declare wkb_cursor binary cursor for " + strSQL);
120
                        ResultSet r =        s.executeQuery("fetch forward all in wkb_cursor");
121
                                    
122
            // ResultSet r = s.executeQuery(strSQL);
123
                        long t2 = System.currentTimeMillis();
124

    
125
                        System.out.println("Tiempo de consulta:" + (t2 - t1) + " milisegundos");
126
                        t1 = System.currentTimeMillis();
127
                        WKBParser parser = new WKBParser();
128
          while( r.next() ) 
129
          { 
130
            /* 
131
            * Retrieve the geometry as an object then cast it to the geometry type. 
132
            * Print things out. 
133
            */ 
134
              // Object obj = r.getObject(1);
135
              byte[] arrayByte = r.getBytes(1);
136
              IGeometry gp = parser.parse(arrayByte);
137
              // String strAux = r.getString(1);
138
              // int id = r.getInt(2);
139
              // System.out.println("Row " + id + ":");
140
              // Geometry regeom = PGgeometry.geomFromString(obj.toString());
141
              // System.out.println(obj.toString());
142
              
143
            // PGgeometry geom = (PGgeometry)obj; 
144
            /* int id = r.getInt(2);
145
            System.out.println("Row " + id + ":"); 
146
            System.out.println(geom.toString()); */ 
147
          }
148
          s.close(); 
149
          conn.close();
150
                        t2 = System.currentTimeMillis();
151

    
152
                        System.out.println("Tiempo de recorrido:"  + (t2 - t1) + " milisegundos");
153
          
154
        } 
155
        catch( Exception e ) 
156
        { 
157
          e.printStackTrace(); 
158
        }  
159
      }
160
}