我的JAVA程序中有以下代码,允许我将文件中的数据复制到Postgres数据库中:
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:####/myDb",
"myuser", "mypassword");
CopyManager cm = new CopyManager((BaseConnection) con);
cm.copyIn("COPY prices FROM STDIN WITH DELIMITER AS ','",
new BufferedReader(new FileReader(filepath)), buffersize);
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,但我想使用连接池来管理我的连接,因为我有这个代码运行多个文件.所以我使用了C3P0.
public static final ComboPooledDataSource cpds = new ComboPooledDataSource();
public class MyPooledConnection {
MyPooledConnection() throws PropertyVetoException {
cpds.setDriverClass("org.postgresql.Driver");
cpds.setJdbcUrl("jdbc:postgresql://localhost:5432/myStockDatabase");
cpds.setUser("myUserName");
cpds.setPassword("myPassword");
cpds.setInitialPoolSize(4);
cpds.setMinPoolSize(4);
cpds.setMaxIdleTime(30);
cpds.setMaxPoolSize(MAX_CONNECTIONS);
}
public static Connection getConnection() {
return cpds.getConnection();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我从上面的连接池获得连接并尝试使用CopyManager时,如下例所示,代码不起作用
Connection pooled_con = MyPooledConnection.getConnection();
CopyManager cm = new CopyManager((BaseConnection) pooled_con);
cm.copyIn("COPY prices FROM STDIN WITH DELIMITER AS …Run Code Online (Sandbox Code Playgroud)