Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / testPostGis.java @ 2269

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

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

    
166
                        System.out.println("Tiempo de recorrido:"  + (t2 - t1) + " milisegundos");
167
          
168
        } 
169
        catch( Exception e ) 
170
        { 
171
          e.printStackTrace(); 
172
        }  
173
      }
174
}