--应该这样package com.gucheng; import java.io.*; import java.sql.*;import oracle.jdbc.driver.OracleDriver;/** * @author yuan * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class conndata {
--应该这样package com.gucheng;
import java.io.*;
import java.sql.*;import oracle.jdbc.driver.OracleDriver;/**
* @author yuan
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class conndata {
public static void main (String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null;
// Get a connection to a database
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.1:1521:ygfdbf",
"pfuser",
"pfuser");
CallableStatement cs=conn.prepareCall("{call FETCHINFOFROMMACHINEID(?,?,?,?,?,?,?,?,?)}");
cs.setInt(1,579);
cs.registerOutParameter(2,java.sql.Types.CHAR);
cs.registerOutParameter(3,java.sql.Types.CHAR);
cs.registerOutParameter(4,java.sql.Types.CHAR);
cs.registerOutParameter(5,java.sql.Types.CHAR);
cs.registerOutParameter(6,java.sql.Types.INTEGER);
cs.registerOutParameter(7,java.sql.Types.INTEGER);
cs.registerOutParameter(8,java.sql.Types.INTEGER);
cs.registerOutParameter(9,java.sql.Types.CHAR);
cs.executeUpdate();
//ResultSet rs = cs.executeQuery();
String para1=cs.getString(2);
}
catch (Exception e){
e.printStackTrace();}
}
}
存储过程的out参数统一为4个。
第一个参数返回查询结果(cursor),第二个返回执行的状态(status code),第三个返回错误号(error code),第四个返回件数。
没用到的为Null。例如:
...
cstm = con.prepareCall(strSQL);
if (vParam != null) {
for (int i = 0; i < vParam.length; i++) {
cstm.setString(i + 5, vParam[i]);
}
}
cstm.registerOutParameter(1, OracleTypes.CURSOR);
cstm.registerOutParameter(2, OracleTypes.NUMBER);
cstm.registerOutParameter(3, OracleTypes.VARCHAR);
cstm.registerOutParameter(4, OracleTypes.NUMBER);boolean bRc = cstm.execute();if (!(cstm instanceof OracleCallableStatement)) {
throw new Exception();
}OracleCallableStatement ocs = (OracleCallableStatement) cstm;try {
rs = ocs.getCursor(1);
} catch (SQLException e) {
UtilDebug.print("cursor未取得");
}status = ocs.getLong(2);
errCode = ocs.getString(3);
recCnt = ocs.getLong(4);
...