copy from 数据流入库
最近更新时间: 2024-10-17 17:10:00
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import java.io.*;
public class copy_from_buff {
public static void main( String args[] )
{
Connection c = null;
Statement stmt = null;
FileInputStream fs = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:11379/postgres","tbase", "tbase");
System.out.println("Opened database successfully");
CopyManager cm = new CopyManager((BaseConnection) c);
String delimiter = new String(";");
StringBuilder buf = new StringBuilder();
//create table t_buff(f1 int,f2 varchar(10));
for (int i=0; i<10000; i++){
buf.append(i + delimiter+ "tbase" + "\r\n");
}
StringReader reader = new StringReader(buf.toString());
cm.copyIn("COPY public.t_buff FROM STDIN WITH DELIMITER ';'", reader);
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Copy data successfully");
}
}