存储过程:列索引无效

时间:2014-06-06 05:50:10

标签: java

(JAVA)我有一个DBConnection类和一个主类。 在DBConnection类中,通过构造函数连接到db。

public class DBConnection {

private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DEFAULT_URL = "jdbc:oracle:thin:@IP:PORT:TesttDb";
private static final String DEFAULT_USERNAME = "est";
private static final String DEFAULT_PASSWORD = "est";
private Connection conn = null;

public DBConnection() {

try {
Class.forName(DEFAULT_DRIVER);
String url = DEFAULT_URL;
Properties props = new Properties();
props.setProperty("user", DEFAULT_USERNAME);
props.setProperty("password", DEFAULT_PASSWORD);
conn = DriverManager.getConnection(url, props);
} 
catch (ClassNotFoundException | SQLException ex) {
System.out.println(ex);
}
}


 public ResultSet getDetails(int id) throws SQLException
    {
        CallableStatement cs;
        cs=conn.prepareCall("{call GET_DETAILS(?,?,?,?)}");
        cs.setInt(1, id);
        cs.registerOutParameter(2, Types.VARCHAR);
        cs.registerOutParameter(3, Types.VARCHAR);
        cs.registerOutParameter(4, Types.INTEGER);
        ResultSet rs=cs.executeQuery();
        System.out.println(cs.getObject(1));

        return rs;

    }

我的主要课程是

public class DBMain {

public static void main(String arg[]) {
try {
DBConnection dBConnection = new DBConnection();
CallableStatement cs=dBConnection.getDetails(1);
System.out.println(cs.getObject(1));
} catch (Exception ex) {
System.out.println(ex);
}
}
}

这里连接正常..

我的存储过程是

CREATE PROCEDURE GET_DETAILS
(
P_ID IN SAMPLE.ID%TYPE,
P_NAME OUT SAMPLE.NAME%TYPE,
P_ADDRESS OUT SAMPLE.ADDRESS%TYPE,
P_AGE OUT SAMPLE.AGE%TYPE
)
AS
BEGIN
SELECT
NAME,ADDRESS,AGE INTO P_NAME,P_ADDRESS,P_AGE
FROM SAMPLE
WHERE SAMPLE.ID=P_ID;
END;
/

我的数据库表是

SQL> desc sample
 Name                            Null?    Type
 ------------------------------- -------- ----
 ID                              NOT NULL NUMBER(5)
 NAME                            NOT NULL VARCHAR2(20
 ADDRESS                         NOT NULL VARCHAR2(20
 AGE                             NOT NULL NUMBER(5)

我在id=1

的表格中有一行

但执行main方法后。我得到了一个例外

run:
java.sql.SQLException: Invalid column index
BUILD SUCCESSFUL (total time: 0 seconds)

任何想法或帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:3)

您正在使用索引2注册out参数。如何使用cs.getXXX(1)检索? 调用cs.getXXX(2)

后必须为cs.executeQuery()

答案 1 :(得分:0)

可以试试

 cs.executeUpdate();
cs.getString(1);