Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中调用存储过程_Java_Postgresql_Stored Procedures_Jdbc_Connection Pooling - Fatal编程技术网

在Java中调用存储过程

在Java中调用存储过程,java,postgresql,stored-procedures,jdbc,connection-pooling,Java,Postgresql,Stored Procedures,Jdbc,Connection Pooling,我正在使用用于PostgreSQL的JDBC驱动程序,我想调用存储过程 假设我有一个调用存储过程的方法,如下所示: public void callProcedure(int someValue) { Connection con = null; try { con = connectionPool.getConnection(); CallableStatement st = con.prepareCall("{ call some_p

我正在使用用于PostgreSQL的JDBC驱动程序,我想调用存储过程

假设我有一个调用存储过程的方法,如下所示:

public void callProcedure(int someValue)    {

    Connection con = null;
    try {
        con = connectionPool.getConnection();

        CallableStatement st = con.prepareCall("{ call some_procedure(?) }");
        st.setInt(1, someValue);
        st.execute();
        st.close();
    }
    catch (SQLException e)  {
        System.out.println(e.getMessage());
    }
    finally     {
        if (con != null) {
            try { con.close(); } // assure connection "goes" back to the pool
            catch (SQLException e)  { }
        }
    }
}
现在让我们假设这个方法
callProcedure
可能被调用数百万次。我的问题是:

(1.)如果我在类的构造函数中创建连接,并在构造函数中准备调用,那么(性能方面)会更好吗

可赎回声明

public Constructor() {
   Connection con = connectionPool.getConnection();
   st = con.prepareCall("{call some_procedure(?)}");
}
然后在方法内部执行以下操作:

   public void callProcedure(int someValue) {
    try {
        st.setInt(1, someValue);
        st.execute();
        st.close();
    }
    catch (SQLException e)  {
        System.out.println(e.getMessage());
    }
    finally     {
        if (con != null) {
            try { con.close(); } // assure connection "goes" back to the pool
            catch (SQLException e)  { }
        }
    }
    }
(2.)总是在执行语句后关闭连接有意义吗?还是让它开着?据我所知,关闭它会将连接返回到连接池,以便其他人可以使用它。对吗

  • 将该语句放入构造函数不会导致性能的提高。您只是在对象生命周期的不同点创建连接

  • 您是对的,关闭连接将立即将其返回到连接池,而不是等待自动发生。这是最好的做法


  • 我尝试这样做已经有一段时间了,但我始终无法从Java连接到我的SQL Server数据库。我有一个存储过程,我想将表中的五个字段加载到JTable中