Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / postgis / testPostGis.java @ 1691

History | View | Annotate | Download (5.32 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.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

    
53
/**
54
 * @author FJP
55
 *
56
 * TODO To change the template for this generated type comment go to
57
 * Window - Preferences - Java - Code Generation - Code and Comments
58
 */
59
public class testPostGis {
60

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

    
68
          
69
      // Tarda casi 28 segundos en recuperar el tema de provincias!!
70
      String dburl = "jdbc:postgresql://localhost/Pruebas";
71
      String dbuser = "postgres";
72
      String dbpass = "aquilina";
73

    
74
      String dbtable = "jdbc_test";
75

    
76
     java.sql.Connection conn; 
77
        try 
78
        { 
79
            System.out.println("Creating JDBC connection...");
80
            Class.forName("org.postgresql.Driver");
81
            Enumeration enumDrivers = DriverManager.getDrivers();
82
            while (enumDrivers.hasMoreElements())
83
            {
84
                System.out.println("Driver " + enumDrivers.nextElement().toString());
85
            }
86
            conn = DriverManager.getConnection(dburl, dbuser, dbpass);
87
            // magic trickery to be pgjdbc 7.2 compatible
88
            // This works due to the late binding of data types in most java VMs. As
89
            // this is more a demo source than a real-world app, we can risk this
90
            // problem.
91
            if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
92
                ((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
93
                ((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
94
            } else {
95
                ((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry");
96
                ((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
97
            } 
98

    
99
            conn.setAutoCommit(false);
100

    
101
          /* 
102
          * Create a statement and execute a select query. 
103
          */
104
            String strSQL = "select ASTEXT(force_2d(the_geom)) as geom from vias";
105
            /* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; 
106
            strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; 
107
            strSQL = strSQL + " WHERE TRUE";
108
            */
109
            // PreparedStatement s = conn.prepareStatement(strSQL);
110
            long t1 = System.currentTimeMillis();
111
            Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
112
            s.setFetchSize(20000);
113
            ResultSet r = s.executeQuery(strSQL);
114
                        long t2 = System.currentTimeMillis();
115

    
116
                        System.out.println("Tiempo de consulta:" + (t2 - t1) + " milisegundos");
117
                        t1 = System.currentTimeMillis();
118
          while( r.next() ) 
119
          { 
120
            /* 
121
            * Retrieve the geometry as an object then cast it to the geometry type. 
122
            * Print things out. 
123
            */ 
124
              // Object obj = r.getObject(1);
125
              // InputStream inS = r.getAsciiStream(1);
126
              String strAux = r.getString(1);
127
              // int id = r.getInt(2);
128
              // System.out.println("Row " + id + ":");
129
              // Geometry regeom = PGgeometry.geomFromString(obj.toString());
130
              // System.out.println(obj.toString());
131
              
132
            // PGgeometry geom = (PGgeometry)obj; 
133
            /* int id = r.getInt(2);
134
            System.out.println("Row " + id + ":"); 
135
            System.out.println(geom.toString()); */ 
136
          }
137
          s.close(); 
138
          conn.close();
139
                        t2 = System.currentTimeMillis();
140

    
141
                        System.out.println("Tiempo de recorrido:"  + (t2 - t1) + " milisegundos");
142
          
143
        } 
144
        catch( Exception e ) 
145
        { 
146
          e.printStackTrace(); 
147
        }  
148
      }
149
}