JDBC调用存储过程实例

来源:互联网 发布:c语言数组遍历 编辑:程序博客网 时间:2024/05/21 01:31
 
import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Types;public class FirstCallable{    public static void main(String[] args) throws ClassNotFoundException, SQLException    {        Connection con = null;        CallableStatement callstmt=null;        try        {            Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.137.23:1521:unieap", "scott", "tiger");//连接数据库            callstmt=con.prepareCall("{call plus_proc(?,?,?)}");            callstmt.setInt(1, 100);            callstmt.setInt(2, 10);            callstmt.registerOutParameter(3, Types.NUMERIC);            callstmt.execute();            int sumNum=callstmt.getInt(3);            System.out.println("运算结果:"+sumNum);        }        catch (ClassNotFoundException e)        {            System.out.println("加载驱动Error");            throw e;        }        catch (SQLException e)        {            System.out.println("access database error");            throw e;        }        finally        {            if (callstmt != null)            {                try                {                    callstmt.close();                }                catch (SQLException e)                {                    e.printStackTrace();                }            }            if (con != null)            {                try                {                    con.close();                }                catch (SQLException e)                {                    e.printStackTrace();                }            }        }    }}

原创粉丝点击