1.使用jar驅動自帶的CopyManager(我這裏使用的是postgresql-9.4.1212.jre7.jar)sql
public class PGPool {
PGPoolingDataSource source = new PGPoolingDataSource();
public PGPool(){
//PG database server name
source.setServerName("數據庫ip");
//PG db port number
source.setPortNumber(端口);
//PG database name
source.setDatabaseName("數據庫");
source.setUser("帳號");
source.setPassword("密碼");
source.setMaxConnections(3);
}
public Connection getConn(){
try{
return source.getConnection();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
@Test
public void testCopy() {
String fileUploadSavePath="C:\\Users\\Administrator.SKY-20170224BTQ\\Desktop\\";
String savedFileName="bendigongcan(1).csv";
String tableName = "smartinsight.test_tpm";
FileInputStream fileInputStream = null;
try {
try {
PGPool pool = new PGPool();
CopyManager copyManager = new CopyManager((BaseConnection)pool.getConn().getMetaData().getConnection());
// 程序csv文件路徑 數據庫
fileInputStream = new FileInputStream(fileUploadSavePath+savedFileName); ssh
//若是導入字段與文件中徹底相同能夠不指定("copy tablename FROM STDIN delimiter ',' csv header encoding 'GBK'")
String sql = "COPY " + tableName + "(province,city,region,cgi,chinesename,covertype,scenario,iscore,iscounty,istown,isvillage,iscityproper,vendor,flag,earfcnflag,isimportantscenario,admin_area,large_scenario,small_scenario,grid,optiroom,islivearea,ishospital,gaosuname,gaotiename,elevate,iszbcj,isjtsn) FROM STDIN delimiter ',' csv header encoding 'GBK'";//指定分隔符和編碼 默認是utf-8程序編碼
log.debug(sql);
copyManager.copyIn(sql, fileInputStream);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}post
2.在數據庫端命令執行本地文件編碼
copy test_tpm from 'C:\\\\Users\\\\Administrator\\\\Desktop\\sshtest\\\\bendigongcan(1).csv'
delimiter ',' csv header encoding 'GBK'; spa