Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / testMySQL.java @ 3476

History | View | Annotate | Download (6.21 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 3476 2005-12-15 16:44:35Z fjp $
48
 * $Log$
49
 * Revision 1.5  2005-12-15 16:44:35  fjp
50
 * Falsa modificaci?n. Solo para pruebas
51
 *
52
 * Revision 1.4  2005/12/15 16:43:47  fjp
53
 * Segunda prueba con las variables del CVS
54
 *
55
 */
56
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql;
57

    
58
import java.sql.DriverManager;
59
import java.sql.ResultSet;
60
import java.sql.Statement;
61
import java.util.Enumeration;
62

    
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.drivers.WKTParser;
65
import com.mysql.jdbc.Driver;
66

    
67

    
68

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

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

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

    
90
      String dbtable = "vias";
91

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

    
136
                        System.out.println("Tiempo de consulta:" + (t2 - t1) + " milisegundos");
137
                        t1 = System.currentTimeMillis();
138
                        int cont = 0;
139
                        WKTParser parser = new WKTParser();
140
          while( r.next() ) 
141
          { 
142
            /* 
143
            * Retrieve the geometry as an object then cast it to the geometry type. 
144
            * Print things out. 
145
            */ 
146
              // Object obj = r.getObject(1);
147
              // InputStream inS = r.getAsciiStream(1);
148
              String strAux = r.getString(1);
149
              // IGeometry geom = parser.read(strAux);
150
              cont++;
151
              // int id = r.getInt(2);
152
              // System.out.println("Row " + id + ":");
153
              // Geometry regeom = PGgeometry.geomFromString(obj.toString());
154
              // System.out.println(obj.toString());
155
              
156
            // PGgeometry geom = (PGgeometry)obj; 
157
            /* int id = r.getInt(2);
158
            System.out.println("Row " + id + ":"); 
159
            System.out.println(geom.toString()); */ 
160
          }
161
          s.close(); 
162
          conn.close();
163
                        t2 = System.currentTimeMillis();
164

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