免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1323 | 回复: 5
打印 上一主题 下一主题

简单的JDBC助手类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-11-22 17:21 |只看该作者 |倒序浏览

  1. import java.sql.*;

  2. public class Helper {
  3.   private Helper() {
  4.   }

  5.   public static Connection getConnection() {
  6.     try {
  7.       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
  8.       String url = "";
  9.       Connection con = DriverManager.getConnection(url, "gongfuxuan",
  10.           "password");
  11.       return con;
  12.     }
  13.     catch (Exception e) {
  14.       e.printStackTrace();
  15.       throw new RuntimeException("Error while get database Connection");
  16.     }
  17.   }

  18.   public static ResultSet query(String sql) {
  19.     Connection con = getConnection();
  20.     ResultSet rs = null;
  21.     try {
  22.       Statement st = con.createStatement();
  23.       rs = st.executeQuery(sql);
  24.       if (st != null) {
  25.         st.close();
  26.       }
  27.       if (con != null) {
  28.         con.close();
  29.       }
  30.     }
  31.     catch (SQLException e) {
  32.       System.out.println("SQL Error,code: " + e.getErrorCode() + ", state:" +
  33.                          e.getSQLState() + "when execute sql:" + sql);
  34.     }

  35.     return rs;
  36.   }

  37.   public static boolean execute(String sql) {
  38.     Connection con = getConnection();
  39.     boolean result = false;
  40.     try {
  41.       Statement st = con.createStatement();
  42.       result = st.execute(sql);
  43.       if (st != null) {
  44.         st.close();
  45.       }
  46.       if (con != null) {
  47.         con.close();

  48.       }
  49.     }
  50.     catch (SQLException e) {
  51.       System.out.println("SQL Error,code: " + e.getErrorCode() + ", state:" +
  52.                          e.getSQLState() + "when execute sql:" + sql);
  53.     }
  54.     return result;
  55.   }

  56.   public static long Update(String sql) {
  57.     Connection con = getConnection();
  58.     long result = -1L;
  59.     try {
  60.       Statement st = con.createStatement();
  61.       result = st.executeUpdate(sql);
  62.       if (st != null) {
  63.         st.close();
  64.       }
  65.       if (con != null) {
  66.         con.close();
  67.       }
  68.     }
  69.     catch (SQLException e) {
  70.       System.out.println("SQL Error,code: " + e.getErrorCode() + ", state:" +
  71.                          e.getSQLState() + "when execute sql:" + sql);
  72.     }
  73.     return result;
  74.   }

  75. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2004-11-22 17:39 |只看该作者

简单的JDBC助手类

呵呵~~,其实可以使用工厂模式,以后就什么数据库都可以通用了

论坛徽章:
0
3 [报告]
发表于 2004-11-22 21:38 |只看该作者

简单的JDBC助手类

有两个问题, 1, 不用连接池, 没什么实用价值, 2, 是又第一个问题导致的, 返回ResultSet必须保持住连接, 直到ResultSet用完才能关闭, 封装得不好。
不过这样设计的想法是很不错的。
如果把ResultSet里的数据导到一个其它的数据结构里,就像CachedRowSet一样, 如何

论坛徽章:
0
4 [报告]
发表于 2004-11-22 22:02 |只看该作者

简单的JDBC助手类

我记得好像说JIVE在这方面处理得比较好!

论坛徽章:
0
5 [报告]
发表于 2004-11-23 10:15 |只看该作者

简单的JDBC助手类

谢谢elgs 指点,对,实际用是肯定要用连接池才行,把
getConnection()函数重写。你指出的第二点,让我很佩服,谢谢,我将修改一下。

论坛徽章:
0
6 [报告]
发表于 2004-11-24 10:06 |只看该作者

简单的JDBC助手类

这种写法我们原来也试过,但是在查询大结果集的时候和有事务处理的时候问题比较多。所以只能不封装了。

要是能有通用的解决方法就好了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP