免费注册 查看新帖 |

Chinaunix

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

实现了一个BufferedResultSet,大家review一下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-09 16:12 |只看该作者 |倒序浏览
本来以为是个技术活,写下去才发现原来是体力的,不过总归是写了,还是贴出来吧

附件是完整的,看出来没,写个基础库多累,100多个方法,每个都要认真实现,正确实现,非吾辈可为啊


  1. import java.io.InputStream;
  2. import java.io.Reader;
  3. import java.math.BigDecimal;
  4. import java.net.URL;
  5. import java.sql.Array;
  6. import java.sql.Blob;
  7. import java.sql.Clob;
  8. import java.sql.Date;
  9. import java.sql.Ref;
  10. import java.sql.ResultSet;
  11. import java.sql.ResultSetMetaData;
  12. import java.sql.SQLException;
  13. import java.sql.SQLWarning;
  14. import java.sql.Statement;
  15. import java.sql.Time;
  16. import java.sql.Timestamp;
  17. import java.util.ArrayList;
  18. import java.util.Calendar;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. public class BufferedResultSet implements ResultSet
  23. {
  24.     private List data;
  25.     private int currentIndex;
  26.     private String[] dbFields;
  27.     public BufferedResultSet(ResultSet rs) throws SQLException
  28.     {
  29.         loadToBuffer(rs);
  30.         this.currentIndex = -1;
  31.     }
  32.     private void loadToBuffer(ResultSet rs) throws SQLException
  33.     {
  34.         if(rs.next())
  35.         {
  36.             ResultSetMetaData metaData = rs.getMetaData();
  37.             int colNum = metaData.getColumnCount();
  38.             this.dbFields = new String[colNum];
  39.             this.data = new ArrayList();
  40.             Map lineMap = new HashMap(colNum);
  41.             for (int i = 0; i < colNum; i++)
  42.             {
  43.                 this.dbFields[i] = metaData.getColumnName(i);
  44.                 lineMap.put(this.dbFields[i],rs.getObject(this.dbFields[i]));
  45.             }
  46.             this.data.add(lineMap);
  47.             while(rs.next())
  48.             {
  49.                 for (int i = 0; i < colNum; i++)
  50.                 {
  51.                     lineMap.put(this.dbFields[i],rs.getObject(this.dbFields[i]));
  52.                 }
  53.                 this.data.add(lineMap);
  54.             }
  55.         }
  56.         else
  57.         {
  58.             this.data = new ArrayList(0);
  59.             this.dbFields = new String[0];
  60.         }
  61.     }
  62.     public boolean next() throws SQLException
  63.     {
  64.         currentIndex++;
  65.         return currentIndex != this.data.size();
  66.     }

  67.     public String getString(int columnIndex) throws SQLException
  68.     {
  69.         Map dataMap = (Map) this.data.get(currentIndex);
  70.         return (String)dataMap.get(this.dbFields[columnIndex]);
  71.     }

  72.     public boolean getBoolean(int columnIndex) throws SQLException
  73.     {
  74.         Map dataMap = (Map) this.data.get(currentIndex);
  75.         return Boolean.parseBoolean((String) dataMap.get(this.dbFields[columnIndex]));
  76.     }

  77.     public int getInt(int columnIndex) throws SQLException
  78.     {
  79.         Map dataMap = (Map) this.data.get(currentIndex);
  80.         return Integer.parseInt((String) dataMap.get(this.dbFields[columnIndex]));
  81.     }
  82.     public String getString(String columnName) throws SQLException
  83.     {
  84.         Map dataMap = (Map) this.data.get(currentIndex);
  85.         return (String)dataMap.get(columnName);
  86.     }

  87.     public boolean getBoolean(String columnName) throws SQLException
  88.     {
  89.         Map dataMap = (Map) this.data.get(currentIndex);
  90.         return Boolean.parseBoolean((String) dataMap.get(columnName));
  91.     }

  92.     public int getInt(String columnName) throws SQLException
  93.     {
  94.         Map dataMap = (Map) this.data.get(currentIndex);
  95.         return Integer.parseInt((String) dataMap.get(columnName));
  96.     }
  97.     public Object getObject(int columnIndex) throws SQLException
  98.     {
  99.         Map dataMap = (Map) this.data.get(currentIndex);
  100.         return dataMap.get(this.dbFields[columnIndex]);
  101.     }

  102.     public Object getObject(String columnName) throws SQLException
  103.     {
  104.         Map dataMap = (Map) this.data.get(currentIndex);
  105.         return dataMap.get(columnName);
  106.     }

  107.     public int findColumn(String columnName) throws SQLException
  108.     {
  109.         int index = -1;
  110.         for (int i = 0; i < this.dbFields.length; i++)
  111.         {
  112.             if(this.dbFields[i].equals(columnName))
  113.             {
  114.                 index = i;
  115.                 break;
  116.             }
  117.         }
  118.         return index;
  119.     }

  120.     //////////////////////////////////ALL unsupport... :)
  121.     public void close() throws SQLException
  122.     {
  123.         throw new SQLException("unsupport......");
  124.     }

  125.     public boolean wasNull() throws SQLException
  126.     {
  127.         throw new SQLException("unsupport......");
  128.     }
  129.    
  130.     // ingore n...
  131.     ....
  132. }
复制代码

BufferedResultSet.rar

1.89 KB, 下载次数: 84

BufferedResultSet

论坛徽章:
0
2 [报告]
发表于 2006-03-09 16:22 |只看该作者
上来现有个疑问:import java.sql.*不行么?为啥些这么许多呢?

另外我还是有点奇怪,方法名起一样就可以了吧。为啥非得实现ResultSet接口呢?

论坛徽章:
0
3 [报告]
发表于 2006-03-09 16:30 |只看该作者
原帖由 艾斯尼勒 于 2006-3-9 17:22 发表
上来现有个疑问:import java.sql.*不行么?为啥些这么许多呢?

另外我还是有点奇怪,方法名起一样就可以了吧。为啥非得实现ResultSet接口呢?


*可以,但规范编码应该是用什么import什么~

实现这个接口主要还是对应那个foxzhu老弟,不然他的其他页面都得改

论坛徽章:
0
4 [报告]
发表于 2006-03-09 16:34 |只看该作者
哦。。明白了

论坛徽章:
0
5 [报告]
发表于 2006-03-09 21:38 |只看该作者
楼上的,如果你用netbeans或者eclipse等开发工具,就可以自动实现import到具体的类。

论坛徽章:
0
6 [报告]
发表于 2006-03-09 21:44 |只看该作者
随然都装了.但由于用作学习用IDE都不合适,一直都vi vi  vi ..哈哈很少使用ide,基本不太会用,以前用过JBUIDER到是真方便

论坛徽章:
0
7 [报告]
发表于 2006-03-09 22:01 |只看该作者
原帖由 perryhg 于 2006-3-9 22:38 发表
楼上的,如果你用netbeans或者eclipse等开发工具,就可以自动实现import到具体的类。


嗯嗯,要不我早就  import *了,连java.sql都不写,,,

论坛徽章:
0
8 [报告]
发表于 2006-03-10 07:57 |只看该作者
所以很多好的编程习惯是需要工具支持的。呵呵

论坛徽章:
0
9 [报告]
发表于 2006-03-10 09:35 |只看该作者
原帖由 sakulagi 于 2006-3-10 08:57 发表
所以很多好的编程习惯是需要工具支持的。呵呵


嗯嗯,效率提高很多:em11:

论坛徽章:
0
10 [报告]
发表于 2006-03-11 10:43 |只看该作者

  1.             while(rs.next())
  2.             {
  3.                 // 原来没写这句,拷贝粘贴总是会出错,,,,
  4.                 lineMap = new HashMap(colNum);
  5.                 for (int i = 0; i < colNum; i++)
  6.                 {
  7.                     lineMap.put(this.dbFields[i],rs.getObject(this.dbFields[i]));
  8.                 }
  9.                 this.data.add(lineMap);
  10.             }
复制代码


讲了review,没人看哦,还是自己发现错了,写完了没测试,,

[ 本帖最后由 kakasi 于 2006-3-11 11:44 编辑 ]

BufferedResultSet.rar

1.89 KB, 下载次数: 89

new version

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP