Guest User

Untitled

a guest
Aug 11th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import java.sql.*;
  2. import au.com.bytecode.opencsv.CSVWriter;
  3. import java.io.*;
  4. import org.postgresql.copy.CopyManager;
  5. import org.postgresql.core.BaseConnection;
  6.  
  7. public class ORtoGP {
  8. public static void main(String[] args) throws SQLException {
  9. try {
  10. String dbURL = "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = xxxxxx)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = xxxxxx) (SRVR = DEDICATED)))";
  11. String strUserID = "xxxxxx";
  12. String strPassword = "xxxxxx";
  13. Connection myConnection=DriverManager.getConnection(dbURL,strUserID,strPassword);
  14. Statement sqlStatement = myConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  15. String readRecordSQL = "select id,name from table where rownum <= 10 ";
  16. ResultSet rs = sqlStatement.executeQuery(readRecordSQL);
  17.  
  18. StringWriter stringWriter = new StringWriter();
  19. CSVWriter csvWriter = new CSVWriter(stringWriter);
  20.  
  21. rs.first();
  22. csvWriter.writeAll(rs, true);
  23. String orresult = stringWriter.toString();
  24. System.out.println(orresult);
  25.  
  26. byte[] bytes = orresult.getBytes();
  27. ByteArrayInputStream orinput = new ByteArrayInputStream(bytes);
  28.  
  29.  
  30. String dbURL1 = "jdbc:postgresql://xxxxx:5432/xxxxx";
  31. String user = "xxxx";
  32. String pass = "xxxx";
  33. Connection conn2 = DriverManager.getConnection(dbURL1, user, pass);
  34.  
  35. CopyManager copyManager = new CopyManager((BaseConnection) conn2);
  36. copyManager.copyIn("copy java_test from stdin with DELIMITER ','",orinput);
  37.  
  38. rs.close();
  39. myConnection.close();
  40. csvWriter.close();
  41.  
  42. } catch (Exception e) {
  43. System.out.println(e);
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment