当前位置: 网站首页 > JAVA > j2ee

J2EE综合--浅析Java程序员的存储过程

时间:1970-1-1 08:33:31来源: j2ee作者:admin 点击:0次 字体 [ С]
这个调用存储过程的Java方法是mapping的一个好例子。Mapping是对一个集上的操作进行抽象的方法。不是在这个过程上返回一个集,我们可以把操作传送进去执行。本例中,操作就是把ResultSet打印到一个输出流。这是一个值得举例的很常用的例子,下面是调用同一个存储过程的另外一个方法实现:

public class ProcessPoetDeaths 

    public abstract void sendDeath(String name, int age); 


static void mapEarlyDeaths(ProcessPoetDeaths mapper) 

    Connection con = null; 
    CallableStatement toesUp = null; 
    try 
    { 
        con = ConnectionPool.getConnection(); 
        con.setAutoCommit(false); 

        CallableStatement toesUp 
            = connection.prepareCall("{ ? = call list_early_deaths () }"); 
        toesUp.registerOutParameter(1, Types.OTHER); 
        getResults.execute(); 

        ResultSet rs = (ResultSet) getResults.getObject(1); 
        while (rs.next()) 
        { 
            String name = rs.getString(1); 
            int age = rs.getInt(2); 
            mapper.sendDeath(name, age); 
        } 
        rs.close(); 
    } 
    catch (SQLException e) 
    { 
        // We should protect these calls. 
        toesUp.close(); 
        con.close(); 
发表评论
验证码:
最新评论
相关文章
最热文章