Postgres CopyManager and connection from Connection Pool

本文介绍了如何在使用数据库连接池的情况下,利用Postgres的CopyManager进行数据的导入和导出。由于CopyManager需要从BaseConnection获取,而从连接池获取的connection不能直接转换为BaseConnection。通过研究发现,可以通过DatabaseMetaData.getConnection()方法得到可以转换的connection。最终,作者展示了如何从连接池中获取连接并执行CopyManager操作的完整代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    最近单位在做oracle向PG数据库的转化.

    其中使用到了PG的CopyManager类来做oracle类似的import和export.快速看下PG CopyManager的例子代码: 

package test.simple;

//You need to include postgres jdbc jar into your project lib
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: leon
 * Date: 13-2-5
 * Time: 下午12:18
 * To change this template use File | Settings | File Templates.
 */
public class TestCopy {
    public static void main(String args[])throws Exception{
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://localhost:5432/postgres";
        Properties props = new Properties();
        props.setProperty("user", "postgres");
        props.setProperty("password", "postgres");
        Connection conn = DriverManager.getConnection(url, props);
        CopyManager cm = new CopyManager((BaseConnection)conn);
        File file = new File("a.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        /*FileWriter fw = new FileWriter(file);

        cm.copyO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值