View Javadoc

1   package migratool.access;
2   
3   import java.sql.Connection;
4   import java.sql.DriverManager;
5   import java.sql.PreparedStatement;
6   import java.sql.SQLException;
7   import java.util.Iterator;
8   import java.util.Vector;
9   
10  /**
11   * 
12   * MIGRATOOL Program to migrate spatial databsets.
13   * Copyright (C) 2007 Fábio Luiz Leite Júnior
14   * Universidade Federal de Campina Grande
15   * contact: fabioleite@gmail.com
16   *
17   * This program is free software; you can redistribute it and/or
18   * modify it under the terms of the GNU General Public License
19   * as published by the Free Software Foundation; either version 2
20   * of the License, or (at your option) any later version.
21   *
22   * This program is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25   * GNU General Public License for more details.
26   *
27   * You should have received a copy of the GNU General Public License
28   * along with this program; if not, write to the Free Software
29   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
30   *
31   * @author fabio
32   *
33   */
34   
35  public class Insertor {
36  	
37  //	Campos
38  	private boolean iniciou;
39  	
40  	//Singleton DEPOIS MELHORAR: criar uma instancia para cada driver
41  	private static Insertor singleton;
42  	
43  	private Insertor() {
44  	}
45  	
46  	/**
47  	 * M�todo do Singleton
48  	 * @return A inst�ncia �nica
49  	*/
50  	public static Insertor getInstance(){
51  		if (singleton == null)
52  			singleton = new Insertor();
53  		return singleton;
54  	}
55  	
56  	/**
57  	  * M�todo utilizado para obten��o de uma conex�o com o Oracle
58  	  * @param dataSource A fonte de dados espec�fica (que BD do SGBD)
59  	  * @return A conex�o
60  	  */
61  	private Connection getConnection(){
62  		Connection con = null;
63  		
64  		String url = "jdbc:oracle:thin:@150.165.75.169:1521:PIRAO";
65  		String user = "";
66  		String password = "";
67  		try{
68  			Class.forName("oracle.jdbc.driver.OracleDriver");
69  			con = DriverManager.getConnection(url,user,password);
70  		}catch(Exception erro){
71  			erro.printStackTrace();
72  		}				
73  		return con;	
74  	  }
75  	
76  	  /**
77  	   * M�todo utilizado para fechar uma conex�o
78  	   * @param con A conex�o a ser fechada
79  	   */
80  	private void closeConnection(Connection con) {
81  		try{
82  			if (!con.isClosed())
83  				con.close();
84  		}catch(Exception erro){
85  			erro.printStackTrace();
86  		}				
87  	}
88  	
89  	public boolean insert(Vector columns, Vector lines, String tableName) throws SQLException{
90  		Insertor inser = Insertor.getInstance();
91  		Connection conn = inser.getConnection();
92  		String variables = generateVariables(columns.size());
93  		PreparedStatement pst = null;
94  		try {
95  			pst = conn.prepareStatement("INSERT INTO " + tableName + " VALUES " + variables);
96  		} catch (SQLException e) {
97  			e.printStackTrace();
98  		}
99  		Iterator itLines = lines.iterator();
100 		while(itLines.hasNext()){
101 			Vector currentLine = (Vector) itLines.next();
102 			for(int parm = 1; parm <= columns.size(); parm++){
103 				pst.setObject(parm,currentLine.elementAt(parm));
104 			} 
105 		}
106 		
107 		return true;
108 	}
109 
110 	/**
111 	 * @param i
112 	 * @return
113 	 */
114 	private String generateVariables(int quantVariable) {
115 		String toReturn = "(?";
116 		for(int variables = 1; variables < quantVariable; variables++){
117 			toReturn = toReturn + ",?";
118 		}
119 		toReturn = toReturn + ")";
120 		return toReturn;
121 	}
122 
123 }