调用存储过程 笔记

调用存储过程 笔记,第1张

概述 *** 作就是把ResultSet打印到一个输出流。这是一个值得举例的很常用的例子,下面是调用同一个存储过程的另外一个方法实现: public class ProcessPoetDeaths{ public abstract void sendDeath(String name, int age); } static void mapEarlyDeaths(ProcessPoet *** 作就是把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); toesUp.execute(); ResultSet rs = (ResultSet) toesUp.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(); } } 

下面是调用该存储过程的Java方法,将结果输出到PrintWriter:

PrintWriter: static voID sendEarlyDeaths(PrintWriter out){ Connection con = null; CallableStatement toesUp = null; try { con = ConnectionPool.getConnection(); // Postgresql needs a transaction to do this... con. setautoCommit(false); // Setup the call. CallableStatement toesUp = connection.prepareCall("{ ? = call List_early_deaths () }"); toesUp.registerOutParameter(1,Types.OTHER); toesUp.execute(); ResultSet rs = (ResultSet) toesUp.getobject(1); while (rs.next()) { String name = rs.getString(1); int age = rs.getInt(2); out.println(name + " was " + age + " years old."); } rs.close(); } catch (sqlException e) { // We should protect these calls. toesUp.close(); con.close(); } } 


存储过程可以帮助你在代码中分离逻辑,这基本上总是有益的。这个分离的好处有:
1 快速创建应用,使用和应用一起改变和改善的数据库模式。
2 数据库模式可以在以后改变而不影响Java对象,当我们完成应用后,可以重新设计更好的模式。
3 存储过程通过更好的sql嵌入使得复杂的sql更容易理解。
4 编写存储过程比在Java中编写嵌入的sql拥有更好的工具--大部分编辑器都提供语法高亮!
5 存储过程可以在任何sql命令行中测试,这使得调试更加容易。

并不是所有的数据库都支持存储过程,但是存在许多很棒的实现,包括免费/开源的和非免费的,所以移植并不是一个问题。Oracle、Postgresql和DB2都有类似的存储过程语言,并且有在线的社区很好地支持。
存储过程工具很多,有像TOAD或TORA这样的编辑器、调试器和IDE,提供了编写、维护PL/sql或pl/pgsql的强大的环境。
存储过程确实增加了你的代码的开销,但是它们和大多数的应用服务器相比,开销小得多。如果你的代码复杂到需要使用DBMS,我建议整个采用存储过程的方式。


摘自:http://www.jb51.cc/article/p-ppktycqi-bko.html

总结

以上是内存溢出为你收集整理的调用存储过程 笔记全部内容,希望文章能够帮你解决调用存储过程 笔记所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/sjk/1178622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存