Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4.  
  5. import org.postgresql.copy.CopyManager;
  6. import org.postgresql.core.BaseConnection;
  7.  
  8. public class PgSqlJdbcCopyStreamsExample {
  9.  
  10. public static void main(String[] args) throws Exception {
  11.  
  12. if(args.length!=4) {
  13. System.out.println("Please specify database URL, user, password and file on the command line.");
  14. System.out.println("Like this: jdbc:postgresql://localhost:5432/test test password file");
  15. } else {
  16.  
  17. System.err.println("Loading driver");
  18. Class.forName("org.postgresql.Driver");
  19.  
  20. System.err.println("Connecting to " + args[0]);
  21. Connection con = DriverManager.getConnection(args[0],args[1],args[2]);
  22.  
  23. System.err.println("Copying text data rows from stdin");
  24.  
  25. CopyManager copyManager = new CopyManager((BaseConnection) con);
  26.  
  27. FileReader fileReader = new FileReader(args[3]);
  28. copyManager.copyIn("COPY t FROM STDIN", fileReader );
  29.  
  30. System.err.println("Done.");
  31. }
  32. }
  33. }
  34.  
  35. this.class.classLoader.rootLoader.addURL('${postgres-jdbc-driver-path}')
  36.  
  37. import java.io.FileReader
  38. import java.sql.DriverManager
  39. import org.postgresql.copy.CopyManager
  40. import org.postgresql.core.BaseConnection
  41.  
  42. new CopyManager( (BaseConnection) DriverManager.getConnection(
  43. '${jdbc-db-url}', '${db-usr}', '${db-usr-pass}' )
  44. ).copyIn( 'COPY t FROM STDIN', new FileReader( '${sqlfile}' ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement