楼主: jaunt

[精华] [TIP]关于Java开发中使用Oracle数据库的一点注意事项(原创)

[复制链接]
论坛徽章:
4
授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34ITPUB元老
日期:2007-05-15 19:49:36秀才
日期:2016-01-13 12:14:26
31#
发表于 2005-5-17 11:25 | 只看该作者
看来要学点java了

使用道具 举报

回复
论坛徽章:
63
19周年集字徽章-19
日期:2020-09-23 02:43:002012新春纪念徽章
日期:2012-02-13 15:12:092012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB十周年纪念徽章
日期:2011-11-01 16:20:28现任管理团队成员
日期:2011-05-07 01:45:082011新春纪念徽章
日期:2011-02-18 11:42:472011新春纪念徽章
日期:2011-01-25 15:42:562011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:41:50
32#
发表于 2005-6-19 01:22 | 只看该作者
最初由 gengmao 发布
[B]通常的jdbc代码:
...
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery('select xx from yy');
...
} catch (SQLExeption e) {
  e.printlStackTrace();
} finally {
  if (stmt != null) {
   try {stmt.close();} catch (SQLException e) { }
  }
} [/B]


关闭顺序不太对,通常的标准的jdbc代码应该是:
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery('select xx from yy');
...
} catch (SQLExeption e) {
  e.printlStackTrace();
} finally {
  if (rs!= null) {
   try {rs.close();} catch (SQLException e) { }
  }if(stmt!=null){
   try {stmt.close();} catch (SQLException e) { }
}if(con!=null){
   try {con.close();} catch (SQLException e) { }
}
}

完全释放掉才是一个好的编程风格

使用道具 举报

回复
论坛徽章:
88
2010年世界杯参赛球队:德国
日期:2010-06-29 10:14:00复活蛋
日期:2011-07-28 13:04:04ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41红宝石
日期:2011-11-10 18:11:58季节之章:夏
日期:2011-11-10 18:15:202012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-01-05 21:19:06祖母绿
日期:2012-05-05 17:09:53灰彻蛋
日期:2012-05-19 15:39:55ITPUB官方微博粉丝徽章
日期:2011-07-01 14:13:35
33#
发表于 2005-7-30 10:25 | 只看该作者
但是我在rs = stmt.executeQuery('select xx from yy');循环内面还要执行stmt.executeUpdate如果关了怎么处理???

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
34#
发表于 2005-8-1 13:55 | 只看该作者
最初由 cc59 发布
[B]

关闭顺序不太对,通常的标准的jdbc代码应该是:
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery('select xx from yy');
...
} catch (SQLExeption e) {
  e.printlStackTrace();
} finally {
  if (rs!= null) {
   try {rs.close();} catch (SQLException e) { }
  }if(stmt!=null){
   try {stmt.close();} catch (SQLException e) { }
}if(con!=null){
   try {con.close();} catch (SQLException e) { }
}
}

完全释放掉才是一个好的编程风格 [/B]

呵呵
connection 经常是要重用的。。。

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
35#
发表于 2005-8-2 14:32 | 只看该作者
最初由 rollingpig 发布
[B]Java连结Oracle常犯错误
1。只懂 createStatement,不懂关闭statement

2.。只懂 createStatement,不懂preparedStatement.

3 。只懂在sql里用to_date,甚至直接用String,不懂用 setDate() [/B]


3、在sql中用to_date,编程简便啊

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
36#
发表于 2005-8-2 14:49 | 只看该作者
最初由 gengmao 发布
[B]Statement每execute一次,就在数据打开一个新的游标。使用多次后再close会有未关闭的游标留下。应该在每次执行并取完记录集后,立即close。 [/B]


Statement每执行一次,不会在数据库打开一个新的游标
数据库参数设置:
SQL> show parameters open_cursors

NAME                                 TYPE    VALUE
------------------------------------ ------- ------------------------------
open_cursors                         integer 300
测试代码如下:
//sqlStmt为java.sql.Statement对象
for(int i=0;i<1000;i++)
{
        sqlStmt.executeQuery("select * from dual";
        System.out.print("looping2222......"+i);
}

未见发生异常
不过我是用的连接池去做的测试
谁有空做一下直连数据库的测试?

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
37#
发表于 2005-8-2 14:51 | 只看该作者
最初由 cc59 发布
[B]

关闭顺序不太对,通常的标准的jdbc代码应该是:
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery('select xx from yy');
...
} catch (SQLExeption e) {
  e.printlStackTrace();
} finally {
  if (rs!= null) {
   try {rs.close();} catch (SQLException e) { }
  }if(stmt!=null){
   try {stmt.close();} catch (SQLException e) { }
}if(con!=null){
   try {con.close();} catch (SQLException e) { }
}
}

完全释放掉才是一个好的编程风格 [/B]


可以不用关闭ResultSet
void java.sql.Statement.close() throws SQLException

Releases this Statement object's database and JDBC resources immediately instead of waiting for
this to happen when it is automatically closed. It is generally good practice to release resources as
soon as you are finished with them to avoid tying up database resources.
Note: A Statement object is automatically closed when it is garbage collected. When a Statement
object is closed, its current ResultSet object, if one exists, is also closed.
抛出:
        SQLException if a database access error occurs

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
38#
发表于 2005-8-2 14:58 | 只看该作者
最初由 RobinOuyang 发布
[B]setdate()为何物,我还一直没有用过,不知那位大侠可以介绍一下。 [/B]


PreparedStatement的方法

/**
     * Sets the designated parameter to a <code<java.sql.Date</code> value.  
         * The driver converts this
     * to an SQL <code>DATE</code> value when it sends it to the database.
     *
     * @param parameterIndex the first parameter is 1, the second is 2, ...
     * @param x the parameter value
     * @exception SQLException if a database access error occurs
     */
    void setDate(int parameterIndex, java.sql.Date x)
            throws SQLException;


/**
     * Sets the designated parameter to the given <code>java.sql.Date</code> value,
         * using the given <code>Calendar</code> object.  The driver uses
         * the <code>Calendar</code> object to construct an SQL <code>DATE</code> value,
         * which the driver then sends to the database.  With a
         * a <code>Calendar</code> object, the driver can calculate the date
         * taking into account a custom timezone.  If no
         * <code>Calendar</code> object is specified, the driver uses the default
         * timezone, which is that of the virtual machine running the application.
     *
     * @param parameterIndex the first parameter is 1, the second is 2, ...
     * @param x the parameter value
         * @param cal the <code>Calendar</code> object the driver will use
         *            to construct the date
     * @exception SQLException if a database access error occurs
         * @since 1.2
     * @see <a href="package-summary.html#2.0 API">What Is in the JDBC
         *      2.0 API</a>
     */
    void setDate(int parameterIndex, java.sql.Date x, Calendar cal)
            throws SQLException;

使用道具 举报

回复
论坛徽章:
23
39#
发表于 2005-8-2 19:44 | 只看该作者
好久没有来了。支持一下。

使用道具 举报

回复
论坛徽章:
63
19周年集字徽章-19
日期:2020-09-23 02:43:002012新春纪念徽章
日期:2012-02-13 15:12:092012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB十周年纪念徽章
日期:2011-11-01 16:20:28现任管理团队成员
日期:2011-05-07 01:45:082011新春纪念徽章
日期:2011-02-18 11:42:472011新春纪念徽章
日期:2011-01-25 15:42:562011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:41:50
40#
发表于 2005-8-2 21:35 | 只看该作者
最初由 rollingpig 发布
[B]
呵呵
connection 经常是要重用的。。。 [/B]


确实是要经常重用.

但是也要看你如何来处理了.不可能在一处循环里面无限次的来使用.那样肯定会出现连接数不够用的情况了.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表