Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_914 / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / testMySQL.java @ 11873

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

    
45
/* CVS MESSAGES:
46
 * 
47
 * $Id: testMySQL.java 11873 2007-05-29 12:35:24Z  $
48
 * $Log$
49
 * Revision 1.6  2006-07-31 06:47:19  jaume
50
 * arregla algunos bugs
51
 *
52
 * Revision 1.5  2005/12/15 16:44:35  fjp
53
 * Falsa modificaci?n. Solo para pruebas
54
 *
55
 * Revision 1.4  2005/12/15 16:43:47  fjp
56
 * Segunda prueba con las variables del CVS
57
 *
58
 */
59
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql;
60

    
61
import java.sql.DriverManager;
62
import java.sql.ResultSet;
63
import java.sql.Statement;
64
import java.util.Enumeration;
65

    
66
import com.iver.cit.gvsig.fmap.drivers.WKTParser;
67

    
68

    
69

    
70
/**
71
 * @author FJP
72
 *
73
 * TODO To change the template for this generated type comment go to
74
 * Window - Preferences - Java - Code Generation - Code and Comments
75
 */
76
public class testMySQL {
77

    
78
      public static void main(String[] args) 
79
      { 
80
      /*    System.err.println("dburl has the following format:");
81
          System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME");
82
          System.err.println("tablename is 'jdbc_test' by default.");
83
          System.exit(1); */
84

    
85
          
86
      // Tarda casi 28 segundos en recuperar el tema de provincias!!
87
      String dburl = "jdbc:mysql://localhost/test";
88
      String dbuser = "root";
89
      String dbpass = "aquilina";
90

    
91
      String dbtable = "vias";
92

    
93
     java.sql.Connection conn; 
94
        try 
95
        { 
96
            System.out.println("Creating JDBC connection...");
97
            Class.forName("com.mysql.jdbc.Driver");
98
            Enumeration enumDrivers = DriverManager.getDrivers();
99
            while (enumDrivers.hasMoreElements())
100
            {
101
                System.out.println("Driver " + enumDrivers.nextElement().toString());
102
            }
103
            conn = DriverManager.getConnection(dburl, dbuser, dbpass);
104
            // magic trickery to be pgjdbc 7.2 compatible
105
            // This works due to the late binding of data types in most java VMs. As
106
            // this is more a demo source than a real-world app, we can risk this
107
            // problem.
108
            /* if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
109
                ((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
110
                ((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
111
            } else {
112
                ((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry");
113
                ((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
114
            } */
115
            System.out.println(conn.getMetaData().getURL());
116
            System.out.println("Drivername = " + conn.getMetaData().getDriverName());
117
            conn.setAutoCommit(false);
118
            System.out.println(conn.getCatalog() + " " + conn.getClass().getName());
119
            java.sql.Driver drv = DriverManager.getDriver(conn.getMetaData().getURL());
120
            System.out.println(drv.getClass().getName());
121
          /* 
122
          * Create a statement and execute a select query. 
123
          */
124
            // String strSQL = "select ogc_geom as geom from vias";
125
            String strSQL = "select ASTEXT(ogc_geom) as geom from " + dbtable;
126
            /* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; 
127
            strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; 
128
            strSQL = strSQL + " WHERE TRUE";
129
            */
130
            // PreparedStatement s = conn.prepareStatement(strSQL);
131
            long t1 = System.currentTimeMillis();
132
            Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
133
            s.setFetchSize(2000);
134
            ResultSet r = s.executeQuery(strSQL);
135
                        long t2 = System.currentTimeMillis();
136

    
137
                        System.out.println("Tiempo de consulta:" + (t2 - t1) + " milisegundos");
138
                        t1 = System.currentTimeMillis();
139
                        int cont = 0;
140
                        WKTParser parser = new WKTParser();
141
          while( r.next() ) 
142
          { 
143
            /* 
144
            * Retrieve the geometry as an object then cast it to the geometry type. 
145
            * Print things out. 
146
            */ 
147
              // Object obj = r.getObject(1);
148
              // InputStream inS = r.getAsciiStream(1);
149
              String strAux = r.getString(1);
150
              // IGeometry geom = parser.read(strAux);
151
              cont++;
152
              // int id = r.getInt(2);
153
              // System.out.println("Row " + id + ":");
154
              // Geometry regeom = PGgeometry.geomFromString(obj.toString());
155
              // System.out.println(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. " + cont + " registros.");
167
          
168
        } 
169
        catch( Exception e ) 
170
        { 
171
          e.printStackTrace(); 
172
        }  
173
      }
174
}