Statistics
| Revision:

root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / testPostGis.java @ 3207

History | View | Annotate | Download (8.62 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.sql.Connection;
47
import java.sql.DriverManager;
48
import java.sql.PreparedStatement;
49
import java.sql.ResultSet;
50
import java.sql.SQLException;
51
import java.sql.Statement;
52
import java.util.Enumeration;
53

    
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser;
56

    
57

    
58
/**
59
 * @author FJP
60
 *
61
 * TODO To change the template for this generated type comment go to
62
 * Window - Preferences - Java - Code Generation - Code and Comments
63
 */
64
public class testPostGis {
65

    
66
      public static void main(String[] args) 
67
      { 
68
      /*    System.err.println("dburl has the following format:");
69
          System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME");
70
          System.err.println("tablename is 'jdbc_test' by default.");
71
          System.exit(1); */
72

    
73
          
74
          String dburl = "jdbc:postgresql://localhost/latin1";
75
          String dbuser = "postgres";
76
          String dbpass = "aquilina";
77
    
78
          String dbtable = "provin";
79
          
80
          Connection conn = null; 
81
          System.out.println("Creating JDBC connection...");
82
          try {
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
          
91
              conn.setAutoCommit(false);
92
          
93
              long t1 = System.currentTimeMillis();
94
              test1(conn, dburl, dbuser, dbpass, dbtable);
95
              long t2 = System.currentTimeMillis();          
96
              System.out.println("Tiempo de consulta1:" + (t2 - t1) + " milisegundos");
97
              
98
              
99
              t1 = System.currentTimeMillis();
100
              test2(conn, dburl, dbuser, dbpass, dbtable);
101
              t2 = System.currentTimeMillis();
102
    
103
              System.out.println("Tiempo de consulta2:" + (t2 - t1) + " milisegundos");
104
               
105
              conn.close();
106

    
107
          } catch (ClassNotFoundException e) {
108
              // TODO Auto-generated catch block
109
              e.printStackTrace();
110
          } catch (SQLException e) {
111
            // TODO Auto-generated catch block
112
            e.printStackTrace();
113
          }
114
          
115
          
116
      }
117
      private static void test1(Connection conn, String dburl, String dbuser, String dbpass, String dbtable)
118
      {          
119
          try 
120
          { 
121
              // magic trickery to be pgjdbc 7.2 compatible
122
              // This works due to the late binding of data types in most java VMs. As
123
              // this is more a demo source than a real-world app, we can risk this
124
              // problem.
125
              /* if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
126
                  ((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
127
                  ((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
128
              } else {
129
                  ((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry");
130
                  ((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
131
              } */
132

    
133
              
134

    
135
            /* 
136
            * Create a statement and execute a select query. 
137
            */
138
              String strSQL = "select AsBinary(the_geom) as geom, nom_provin from " + dbtable;
139
              // String strSQL = "select ASBINARY(the_geom) as geom from " + dbtable;
140
              // strSQL = "select ASTEXT(the_geom), nom_provin as geom from " + dbtable;
141
              /* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; 
142
              strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; 
143
              strSQL = strSQL + " WHERE TRUE";
144
              */
145
              // PreparedStatement s = conn.prepareStatement(strSQL);
146
              
147
              Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
148
            // s.execute("begin");
149
              
150
              // Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
151
              // s.setFetchSize(5);
152
              int fetchSize = 5;
153
            s.execute("declare wkb_cursor binary cursor for " + strSQL);
154
            ResultSet r =   s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor");
155
              /// ResultSet r =   s.executeQuery("fetch forward all in wkb_cursor");
156
                        
157
              // ResultSet r = s.executeQuery(strSQL);
158
            WKBParser parser = new WKBParser();
159
            long id=0;
160
            /* for (int i=1; i< 100; i++)
161
            {
162
                r.absolute(i);
163
                System.out.println("Row " + i + ":" + r.getString(2));
164
            }
165
            r.beforeFirst();
166
              for (int i=1; i< 100; i++)
167
              {
168
                  r.absolute(i);
169
                  System.out.println("Row " + i + ":" + r.getString(2));
170
              } */
171
              
172
            while( r.next() ) 
173
            { 
174
              /* 
175
              * Retrieve the geometry as an object then cast it to the geometry type. 
176
              * Print things out. 
177
              */ 
178
                // Object obj = r.getObject(2);
179
                byte[] arrayByte = r.getBytes(1);
180
                IGeometry gp = parser.parse(arrayByte);
181
                String strAux = r.getString(2);
182
                // int id = r.getInt(2);
183
                // System.out.println("Fila " + id + ":" + obj.toString());
184
                id++;
185
                // Geometry regeom = PGgeometry.geomFromString(obj.toString());
186
                
187
              // PGgeometry geom = (PGgeometry)obj; 
188
               // int id = r.getInt(2);
189
              System.out.println("Row " + id + ":" + strAux); 
190
              // System.out.println(geom.toString()); 
191
                // System.out.println("provin=" + r.getString(2));
192
                if ((id % fetchSize) == 0)
193
                {
194
                    r =   s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor");
195
                }
196
                
197
            }
198
            s.close();             
199
          } 
200
          catch( Exception e ) 
201
          { 
202
            e.printStackTrace(); 
203
          }  
204
        }
205
      private static void test2(Connection conn, String dburl, String dbuser, String dbpass, String dbtable)
206
      {          
207
          try 
208
          { 
209
            /* 
210
            * Create a statement and execute a select query. 
211
            */
212
              String strSQL = "select gid from " + dbtable;
213
              PreparedStatement s = conn.prepareStatement(strSQL);
214
              
215
              // Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
216
              int fetchSize = 5000;
217
              ResultSet r = s.executeQuery(strSQL);
218
              int id=0;
219
              while( r.next() ) 
220
              { 
221
                String strAux = r.getString(1);
222
                id++;
223
                // System.out.println("Row " + id + ":" + strAux); 
224
              } 
225
              s.close();
226
          } 
227
          catch( Exception e ) 
228
          { 
229
            e.printStackTrace(); 
230
          }  
231
        }
232
      
233
}