2 u013863751 u013863751 于 2015.05.25 14:25 提问

Java调用存储国过程报错

调用代码如下,求大神指正

 package com.lofter.svntesr;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbcp.DelegatingConnection;

import oracle.jdbc.OracleTypes;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;

import com.lofter.bean.ProcedureBean;
import com.lofter.dao.MyProperties;

public class ProcedureTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String user = "scott";
        String pwd = "a";

        Connection conn = null;
        CallableStatement cs = null;
        ResultSet rs = null;

        List<ProcedureBean> list = new ArrayList<ProcedureBean>();
        for (int i = 1; i <= 5; i++) {
            String r = i + "";
            list.add(new ProcedureBean(r, r, "0"));
        }
        try {
            Class.forName( MyProperties.getInstance().getProperty("driverClassName"));
            DataSource ds=BasicDataSourceFactory.createDataSource(  MyProperties.getInstance());
            conn=ds.getConnection();
            System.out.println(conn);

//这句老是报 :Message file 'oracle.jdbc.driver.Messages' is missing
            StructDescriptor recDesc = StructDescriptor.createDescriptor(
                    "type_array", ((DelegatingConnection)conn).getDelegate());

            ArrayList<STRUCT> pstruct = new ArrayList<STRUCT>();
            for (ProcedureBean pb : list) {
                Object[] objs = new Object[3];
                objs[0] = pb.getUserid();
                objs[1] = pb.getTopicid();
                objs[2] = pb.getRecord();
                STRUCT item = new STRUCT(recDesc, conn, objs);
                pstruct.add(item);
            }

            ArrayDescriptor ad = ArrayDescriptor.createDescriptor(
                    "type_array_tbl", conn);
            oracle.sql.ARRAY array = new oracle.sql.ARRAY(ad, conn, pstruct.toArray());

            cs = conn.prepareCall("{call readRecord(?,?)}");
            cs.setArray(1, array);
            cs.registerOutParameter(2, OracleTypes.CURSOR);
            cs.execute();
            rs = (ResultSet) cs.getObject(2);
            while (rs.next()) {
                System.out.println("userid:" + rs.getString(1) + "\t"
                        + "topicid:" + rs.getString(2) + "\t" + "record:"
                        + rs.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    if (cs != null) {
                        cs.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }


}

2个回答

ddwhan0123
ddwhan0123   2015.05.27 09:48

看看jar包引入有没有少东西,看看驱动jar包对不对

u013863751
u013863751   2015.05.27 18:00

怎么看,不是ojdbc14吗?

Csdn user default icon
上传中...
上传图片
插入图片