Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » 技术文章库
打印话题 寄给朋友 订阅主题 |
作者 | Re:JDBC revisit [Re:floater] |
floater
Java Jedi 总版主 发贴: 3233 积分: 421 |
于 2003-09-21 11:47
http://www.cjsdn.com/post/view?bid=21&id=48838 I took a look at the source code and gave out some comments in the above post. In this post, let's see how to fix the problems I mentioned. Basically, we are taking a refactoring approach, the bottom line is that if the code is too long(I use the rule: every class file shouldn't go beyond 500 lines, and I haven't seen exceptions in application development). 1. The connection pool should be out, in a seperate package. What we pass into here should be just a connection because that's all it's related. In fact, the connection pool and datasource should have its own package, independent of everything else. So we pass the Connection object to the constructor. 2. Transaction handling is also connection related, so we leave that out in a seperate class. 3. The error handling is kind of wicky. It has a bunch of functions to check what the root cause is. Comm'n, I don't want to go through each call to know exactly what's wrong, just tell me what it is, I am a lazy guy. Besides, if we know the error, we won't be able to do anything in the code(with if statements). So we change it to a simple msg. 4. Continue in error handling, we really don't need subclasses to translate any specific db error code. We could, instead, use a properties file to map the error codes to messages. This saves users from programming. So we add a file name to the constructor. 5. If we take a bird view of the main class, it has a big chunk of code for SQL parameter settings. We could move them out of here. There is another big chunk of code dealing with converting from primitive typs to objects, so let's move this piece out too. 6. The SQLResult class is mixed with implemention interface with user interface, so we should seperate them too(input/output). Having done these, we have something like this file tree: (ignore the files with names starting with Test..., they are for testing purpose).
why edited on 2003-10-26 10:33
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler, Refactoring - Improving the Design of Existing Code |
作者 | Re:JDBC revisit [Re:floater] |
floater
Java Jedi 总版主 发贴: 3233 积分: 421 |
于 2003-09-21 12:22
The source code is attached, but haven't got time to fully test it(so use it in your risk). From the original SQLExecutor package, we simplify some(exceptions), modify some(seperate/isolate dependencies), the purpose is to write something maintainable: 1. Seperate transaction/connection pool, so we could replace them with something better later. 2. Use List interface, so we could change from ArrayList to something else later. Use sqlrunner.sqlresult.QueryResultGenerator so we know what we need to generat the result. So these interfaces act as firewall in case we have a fire in one piece of the code and it can't populate to elsewhere. 3. Users of these package should care only the top classes in sqlrunner, so there is no need for them to dig into subpackages(implementation details). These are the considerations that a mature developers should consider and should do. A better version down further. Is there a way to delete this file? { there's a checkbox "Delete old attachment sqlrunner.rar" at the bottom -- why } sqlrunner.rar (13.63k)
why edited on 2003-10-26 17:20
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler, Refactoring - Improving the Design of Existing Code |
作者 | Re:JDBC revisit [Re:floater] |
Johnny
I will rock you 发贴: 87 积分: 10 |
于 2003-09-22 09:44
老大,第二段source里面resultset和statement好像没关么? |
作者 | Re:JDBC revisit [Re:floater] |
Johnny
I will rock you 发贴: 87 积分: 10 |
于 2003-09-25 10:50
??what's you mean? I mean you never closed the rs and ps. |
作者 | Re:JDBC revisit [Re:floater] |
floater
Java Jedi 总版主 发贴: 3233 积分: 421 |
于 2003-09-28 04:49
Here is a better version: I tested this on MySQL and Sybase for SQL statements. No stored procedure testing yet. sqlrunner.rar (13.82k) "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler, Refactoring - Improving the Design of Existing Code |
作者 | Re:JDBC revisit [Re:floater] |
floater
Java Jedi 总版主 发贴: 3233 积分: 421 |
于 2003-09-28 04:54
You need to have JDBC knowledge and general J2SE knowledge, nothing else. Does anyone know how to add line numbers to the code in a word document?(only a portion of a page, not the whole page). Thanks if you like me know. design.doc (87.5k) "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler, Refactoring - Improving the Design of Existing Code |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |