View Javadoc

1   package migratool.access;
2   
3   import java.sql.Connection;
4   import java.sql.DriverManager;
5   import java.sql.ResultSet;
6   import java.sql.ResultSetMetaData;
7   import java.sql.SQLException;
8   import java.sql.Statement;
9   import java.util.Iterator;
10  import java.util.Vector;
11  
12  /**
13   * 
14   * MIGRATOOL Program to migrate spatial databsets.
15   * Copyright (C) 2007 Fábio Luiz Leite Júnior
16   * Universidade Federal de Campina Grande
17   * contact: fabioleite@gmail.com
18   *
19   * This program is free software; you can redistribute it and/or
20   * modify it under the terms of the GNU General Public License
21   * as published by the Free Software Foundation; either version 2
22   * of the License, or (at your option) any later version.
23   *
24   * This program is distributed in the hope that it will be useful,
25   * but WITHOUT ANY WARRANTY; without even the implied warranty of
26   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27   * GNU General Public License for more details.
28   *
29   * You should have received a copy of the GNU General Public License
30   * along with this program; if not, write to the Free Software
31   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
32   *
33   * @author fabio
34   * 
35   * Classe que acessa os dados do banco de dados fonte
36   *
37   */
38   
39  public class Extractor {
40  	//	Campos
41  	private boolean iniciou;
42  	
43  	//Singleton DEPOIS MELHORAR: criar uma instancia para cada driver
44  	private static Extractor singleton;
45  	
46  	private Extractor() {
47  	}
48  	
49  	/**
50  	 * M�todo do Singleton
51  	 * @return A inst�ncia �nica
52  	*/
53  	public static Extractor getInstance(){
54  		if (singleton == null)
55  			singleton = new Extractor();
56  		return singleton;
57  	}
58  	
59  	/**
60  	  * M�todo utilizado para obten��o de uma conex�o com o Oracle
61  	  * @param dataSource A fonte de dados espec�fica (que BD do SGBD)
62  	  * @return A conex�o
63  	  */
64  	public Connection getConnection(){
65  		Connection con = null;
66  		
67  		String url = "jdbc:odbc:db2";
68  		String user = "";
69  		String password = "";
70  		try{
71  			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
72  			con = DriverManager.getConnection(url,user,password);
73  		}catch(Exception erro){
74  			erro.printStackTrace();
75  		}				
76  		return con;	
77  	  }
78  	
79  	  /**
80  	   * M�todo utilizado para fechar uma conex�o
81  	   * @param con A conex�o a ser fechada
82  	   */
83  	public void closeConnection(Connection con) {
84  		try{
85  			if (!con.isClosed())
86  				con.close();
87  		}catch(Exception erro){
88  			erro.printStackTrace();
89  		}				
90  	}
91  	
92  	public static void main(String [] args){
93  		Extractor ext = Extractor.getInstance();
94  		Connection conn = ext.getConnection();
95  		Statement stmt;
96  		ResultSet rs;
97  
98  		try {
99  		    stmt = conn.createStatement();
100 		    rs = stmt.executeQuery("select * from empregado");
101 		    
102 			Vector nomeColunas = new Vector();
103 			Vector linhas = new Vector();
104 		    ResultSetMetaData rsmd = rs.getMetaData();
105 			for (int i =1; i <= rsmd.getColumnCount(); ++i)		           
106 	   			nomeColunas.addElement(rsmd.getColumnName(i));
107 			while (rs.next()){
108 				linhas.addElement(getProximaLinha(rs, nomeColunas));
109 			}
110 			
111 			Iterator itNomeColunas = nomeColunas.iterator();
112 			while(itNomeColunas.hasNext()){
113 				String nomeColunaI = (String)itNomeColunas.next();
114 				System.out.print(nomeColunaI + " | ");
115 			}
116 
117 			System.out.println("\n--------------------------");
118 			Iterator itlinhas = linhas.iterator();
119 			while(itlinhas.hasNext()){
120 				Vector dadoLinhaI = (Vector)itlinhas.next();
121 				Iterator itDados = dadoLinhaI.iterator();
122 				while(itDados.hasNext()){
123 					String dados = itDados.next().toString();
124 					System.out.print(dados + " | ");
125 				}
126 				System.out.println();
127 			}
128 		   stmt.close();
129 		}
130 		catch (SQLException sqlex) {
131 			sqlex.printStackTrace();
132 		}
133 		ext.closeConnection(conn);
134 	}
135 	
136 	private static Vector getProximaLinha( ResultSet rs, Vector rsmd) throws SQLException{
137 		Vector linhaCorrente =  new Vector();
138 		System.out.println(rsmd.size());
139 		Iterator itRsmd = rsmd.iterator();
140 		while(itRsmd.hasNext()){
141 			linhaCorrente.addElement(rs.getObject((String)itRsmd.next()));
142 		}
143 		/*
144 		for (int i = 1; i <= rsmd.size(); i++){
145 			
146 			switch (rsmd.getColumnType(i)) {
147 				case Types.VARCHAR:
148 					linhaCorrente.addElement(rs.getString(i));   
149 					break;
150 			  	case Types.INTEGER:
151 					linhaCorrente.addElement(new Integer(rs.getInt(i)));
152 		 			break;
153 			  	case Types.FLOAT:
154 					linhaCorrente.addElement(new Double(rs.getDouble(i)));
155 				   	break;
156 			  	default: 
157 				System.out.println("Tipo foi:" + rsmd.getColumnTypeName(i));
158 			}
159 		}*/
160 		return linhaCorrente;
161 	}
162 
163 	
164 }