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 copyfrom {
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:15432/postgres?binaryTransfer=false","tbase", "tbase");
System.out.println("Opened database successfully");
CopyManager cm = new CopyManager((BaseConnection) c);
fs = new FileInputStream("/data/tbase/tbase.csv");
String sql = "COPY public.tbase FROM STDIN DELIMITER AS ','";
cm.copyIn(sql, fs);
c.close();
fs.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Copy data successfully");
}
}