Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java EE (J2EE) и Spring > Вызов хранимой процедуры с параметрами


Автор: Devider 10.6.2009, 11:55
Проблема такая. Надо вызвать ХП (постгрес) вида 
Цитата

CREATE OR REPLACE FUNCTION getallprods(args integer[])

 Долго мучался, в итоге получилось так:
Цитата

String driverName = "org.postgresql.Driver"; 
Class.forName(driverName); 
Connection cn = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/mydb","postgres","passw");
Object[] param = {1,2,3,4};
Array array = connection.createArrayOf("integer", param);
CallableStatement proc = connection.prepareCall("{ call getallprods(?) }");
proc.setArray(1, array);
proc.execute();


Все бы ничего но если использовать пул соединений типа
Цитата

Context ctx = new InitialContext(); 
if(ctx == null ) throw new Exception("No Context"); 
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/postgres"); 
Connection cn = ds.getConnection(); 
Object[] param = {1,2,3,4};
Array array = connection.createArrayOf("integer", param);

то на последней строчке валится 
Цитата

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 57

54:                     Object[] o = {1,2,3,4,5};
55:                     String classname = "org.postgresql.Driver";
56:                     Class.forName(classname);
57:                     Array array = conn.createArrayOf("smallint", o);
58: 
59:      conn.close();
60:     }


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


root cause 
javax.servlet.ServletException: java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array;
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:156)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause 
java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array;
    org.apache.jsp.index_jsp._jspService(index_jsp.java:121)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


Что с этим делать?

Автор: AntonSaburov 10.6.2009, 16:22
Хорошо бы посмотреть как ты конфигуришь context.xml и web.xml.

Но судя по сообщению - может драйвер это не тянет ?

Автор: Devider 10.6.2009, 16:52
context.xml:
Цитата

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/WebApplication">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Resource name="jdbc/postgres" auth="Container"
        type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://127.0.0.1:5432/mydb"
        username="postgres" password="mypassw" maxActive="20" maxIdle="10" maxWait="-1"/>

</Context>


web.xml
Цитата

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;&gt;
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <description>postgreSQL Datasource example</description>
    <resource-ref>
        <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>



Автор: kirillmana 11.6.2009, 07:32
Вот вызов Оракловой функции, но принцип тот же

Есть DbConnect.java
Код

package comstar.check_security.connect;

import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;

public class DbConnect  {

    public static boolean useApplicationServer = false;
    public static String connectPool = "jdbc/ora_db";
    public static String defaultFileCharsetName = "windows-1251";
    
    public static Connection getConnect() throws Exception{
        if (useApplicationServer) {
            return getPoolConnect(connectPool);
        } else {
            return getDebugConnect();
        }
    }

    public static Connection getPoolConnect(String connectPool) throws Exception {         
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:/comp/env/" + connectPool);
        return ds.getConnection();
    }

    public static Connection getDebugConnect() throws Exception {
        DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
        Connection cn = DriverManager.getConnection("jdbc:oracle:thin:@db_host:1600:dbhost","WWWGUI", "***");
        return cn;
    }
}


А вот сам вызов
Код

Connection conn = null;
CallableStatement cstmt = null;
ResultSet rs = null;
try {
    conn = DbConnect.getConnect();
    cstmt = conn.prepareCall("begin ?:=Pk54_Monitor_Grx.ora_func(); end;");
    cstmt.registerOutParameter(1, Types.INTEGER);
    cstmt.registerOutParameter(2, Types.VARCHAR);
    cstmt.registerOutParameter(3, OracleTypes.CURSOR);
    cstmt.setInt(4, this.getId());
    cstmt.setString(5, p_date_scale);
    cstmt.execute();
    rs = (ResultSet) cstmt.getObject(3);
    //TODO
} catch (SQLException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {if (rs!=null){rs.close();} }catch (SQLException e) {e.printStackTrace();}
    try {if (cstmt!=null){cstmt.close();} }catch (SQLException e) {e.printStackTrace();}
    try {if (conn!=null){conn.close();} }catch (SQLException e) {e.printStackTrace();}
}


Автор: Devider 11.6.2009, 10:44
Спасибо, но проблема не с самим вызовом, проблема с передачей массива в параметрах..

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)