Last active
August 29, 2015 14:04
-
-
Save JRGGRoberto/4a34ee5edf62eff45833 to your computer and use it in GitHub Desktop.
Ler Oracle insere MySQL - parametros (nome_tabela, campo_pk, n_indice_inicial) - necessário mysql-connector-java-5.0.8-bin.jar e ojdbc6.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Este programa serve para importar do Oracle para o MySQL | |
A base de dados e tabelas do lado que receberá a importação já deverá existir | |
Este Não! verifica se o registro, não evita duplicações | |
*/ | |
package basededasosnofiltro; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
/** | |
* | |
* @author rgomes Gomes J. Roberto Góes JRGG | |
*/ | |
public class BasededasosNoFiltro { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
String tabela = "OCSPT_WF_INCIDENT_MODEL"; | |
String pkcampo = null; | |
String betwee = null; | |
String limite = null; | |
if(args.length != 0){ | |
tabela = args[0]; | |
if(args[1].length() != 0){ | |
pkcampo = args[1]; | |
betwee = args[2]; | |
limite = " where "+pkcampo+" between "+betwee+" and "+betwee+"+5000"; | |
} | |
} else { | |
// System.exit(0); | |
System.out.println(tabela); | |
} | |
Connection con = null; | |
try { | |
// String url = "jdbc:mysql://localhost:33307/otscrm"; // Meu PC GMtel | |
// String url = "jdbc:mysql://192.168.1.151:3306/otscrm"; // Servidor de integrações DEV | |
// String url = "jdbc:mysql://localhost:33307/jogos_ccenter"; // Meu PC Gmtel | |
// String url = "jdbc:mysql://192.168.1.23:3306/otscrm"; // Servidor Vtiger DEV base OTSCRM | |
// String url = "jdbc:mysql://192.168.1.209:3306/otscrm"; // VM Rafael | |
// String url = "jdbc:mysql://10.100.62.27:3306/jogos_ccenter"; // Middle Qualidade SCML | |
// String url = "jdbc:mysql://10.100.61.23:3306/jogos_ccenter"; // Middle Qualidade SCML | |
String url = "jdbc:mysql://10.100.63.33:3306/otscrm"; // Meu PC Gmtel | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection conexao = DriverManager.getConnection(url, "root", "20gmtelSQL14"); | |
// Connection conexao = DriverManager.getConnection(url, "root", "20gmtel14"); | |
// Connection conexao = DriverManager.getConnection(url, "root", "20middleSQL14"); | |
System.out.println("Conexao MySQL ok!!!"); | |
Class.forName("oracle.jdbc.OracleDriver"); | |
System.out.println("Oracle JDBC driver loaded ok."); | |
con=DriverManager.getConnection("jdbc:oracle:thin:apps/apps@10.50.100.103:1521:PROD"); // Produção | |
// con=DriverManager.getConnection("jdbc:oracle:thin:apps/apps@10.50.100.70:1611:TEST"); // Afrodite | |
System.out.println("Conectado Oracle"); | |
int qntCampos = 0; | |
String qryCamp = "select column_name, data_type from all_tab_cols where table_name = upper('"+tabela+"')and column_name not in('DATABASE') and column_id is not null"; | |
java.sql.Statement stmtcamp = con.createStatement(); | |
ResultSet rowsCamp = stmtcamp.executeQuery(qryCamp); | |
ArrayList<String> collumnname = new ArrayList<String>(); | |
// ArrayList<String> collumntype = new ArrayList<String>(); | |
while (rowsCamp.next()) { | |
collumnname.add(rowsCamp.getString("column_name")); | |
//collumntype.add(rowsCamp.getString("data_type")); | |
qntCampos++; | |
} | |
System.out.println("Qntdd campos: "+qntCampos); | |
System.out.println(); | |
String qryselect = ""; | |
String qryinsert = ""; | |
for (int i=0; i < collumnname.size(); i++) { | |
qryinsert = qryinsert + collumnname.get(i); | |
if (i < collumnname.size()-1){ | |
qryinsert = qryinsert +", "; | |
} | |
} | |
String insert = ""; | |
for (int x=1; x< qntCampos; x++){ | |
insert = insert + ", ?"; | |
} | |
qryinsert = "insert into "+tabela+" ("+qryinsert+" ) values (?"+insert + ")"; | |
collumnname.clear(); | |
qryselect = ""; | |
qryCamp = "select decode(data_type,'DATE','to_char('||column_name||',''YYYY-MM-DD HH24:MI:SS'')',column_name) from all_tab_cols where table_name = upper('"+tabela+"')and column_name not in('DATABASE') and column_id is not null"; | |
java.sql.Statement stmtcamp2 = con.createStatement(); | |
ResultSet rowsCamp2 = stmtcamp2.executeQuery(qryCamp); | |
while (rowsCamp2.next()) { | |
collumnname.add(rowsCamp2.getNString(1)); | |
} | |
for (int y=0; y < collumnname.size(); y++) { | |
qryselect = qryselect + collumnname.get(y); | |
if (y < collumnname.size()-1){ | |
qryselect = qryselect +", "; | |
} | |
} | |
qryselect = "SELECT "+qryselect+" from "+tabela+limite; | |
System.out.println(qryselect); | |
System.out.println(); | |
System.out.println(qryinsert); | |
System.out.println(); | |
java.sql.Statement stmtcampA = con.createStatement(); | |
ArrayList<String> campo = new ArrayList<String>(); | |
ResultSet rowsCampA = stmtcampA.executeQuery(qryselect); | |
qntCampos++; | |
while (rowsCampA.next()) { | |
for(int i=1; i < qntCampos; i++){ | |
campo.add(rowsCampA.getNString(i)); | |
} | |
Statement stmtMysql = conexao.createStatement(); | |
String sql = qryinsert; | |
PreparedStatement statement = conexao.prepareStatement(sql); | |
for(int i=1; i < qntCampos; i++){ | |
statement.setString(i, campo.get(i-1)); | |
} | |
campo.clear(); | |
int rowsInserted = statement.executeUpdate(); | |
} | |
conexao.close(); | |
con.close(); | |
System.out.println("Importação OK - Desconectado"); | |
} catch (Exception e) { | |
System.err.println("Exception: "+e.getMessage()); | |
System.err.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
java -jar BasededasosNoFiltro.jar jtf_ih_activities activity_id 58118
java -jar args[0] args[2] args[2]
Onde
args[0] = nome da tabela
args[1] = nome do campo_pk
args[1] = valor inicial do campo_pk
com isso será selecionado todos os campos where campo_pk between valor inicial and valor inicial+5000