import java.sql.*;
import org.postgresql.*;

public class PgSqlJdbcCopyStreamsExample {

    public static void main(String[] args) throws Exception {

        if(args.length!=3) {
            System.out.println("Please specify database URL, user and password on command line.");
            System.out.println("Like this: jdbc:postgresql://localhost:5432/test test password");
        } else {

            System.err.println("Loading driver");
            Class.forName("org.postgresql.Driver");

            System.err.println("Connecting to " + args[0]);
            Connection con = DriverManager.getConnection(args[0],args[1],args[2]);

            System.err.println("Creating temporary table");
            con.createStatement().execute("CREATE TEMPORARY TABLE t(t text)");

            System.err.println("Copying text data rows from stdin");
            ((PGConnection)con).getCopyAPI().copyIntoDB("COPY t FROM STDIN", System.in);

            System.err.println("Copying temporary table contents to stdout");
            ((PGConnection)con).getCopyAPI().copyFromDB("COPY t TO STDOUT", System.out);

            System.err.println("Done.");
        }
    }
}