请帮我从JDBC调用存储过程

时间:2011-07-23 04:18:35

标签: jdbc

这是我的JDBC程序

public class TestOCIApp {

    public static void main(String args[]) throws ClassNotFoundException,
            SQLException {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcle", "scott", "tiger");

            CallableStatement cs = conn.prepareCall("{call emp_name(?,?)}");

            cs.registerOutParameter(2, java.sql.Types.INTEGER);
            cs.setInt(1, 7934);

            ResultSet rs = cs.executeQuery();
                while (rs.next())
                System.out.println(rs.getString(1));
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的存储过程

 CREATE OR REPLACE PROCEDURE emp_name (id IN NUMBER, emp_name OUT NUMBER)
   IS

   BEGIN

      SELECT ename INTO emp_name

      FROM emp WHERE empno = id;

   END;

1 个答案:

答案 0 :(得分:0)

您应该按以下方式调用存储过程:

            cs.execute();
            ResultSet rs = (ResultSet)cs.getObject(1);