站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题将数据库操作封装到Javabean

将数据库操作封装到Javabean

摘要: 将数据库操作封装到Javabean

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="54" align="center" valign="top"> </td> </tr> <tr> <td height="20">
封装数据库操作,目的就是为了隐藏java.sql包内的类,在编码中去掉核心的数据库操作代码。以杜绝直接数据库操作容易带来的资源未释放问题。同时也减少了数据库操作的编码量。

但是很多网友在封装时,却喜欢返回结果集(ResultSet对象),那么这个封装就没有意义了。

1. 又是直接操作核心数据库类,跟封装前几乎没什么变化。
2. 结果集总是依赖于它使用的连接(Connection)对象。因此当连接对象在方法内被关闭后,你返回的ResultSet就没有用了。

如果真的要获得查询数据库的结果集,就把结果集对象内的所有数据,转储到以Map为元素的List对象内。
当然,这种方式,不能适应大数据量的查询,不过如果真的碰到大数据量的查询,那用什么封装都不好,还是得直接数据库操作. :)))

下面是简单的数据库操作Javabean的代码:

<table border=1> <tr> <td width="653">DbWrapper.java</td> </tr> <tr> <td>
import java.sql.*;

import java.util.*;

public class DbWrapper

{

    // 定义连接池对象为静态变量,将一直存在,直到工作目录关闭。

    private static DataSource ds = null;
// 1.用连接池的方式获得连接 // 如果不是做多数据库程序,推荐使用此方法 // 相关内容:在tomcat管理界面配置连接池 public static Connection openConnection() throws Exception { // 只需要初始化1次 if ( ds == null ) { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource ds = (DataSource) envContext.lookup("jdbc/MyDataSource"); } return ds.getConnection(); } // 2.用jdbc驱动获得连接 // 相关内容:JSP数据库连接大全 public static Connection openConnection( String driver, String url, String username, String password) throws Exception { Class.forName(driver).newInstance(); return DriverManager.getConnection(url, username, password); } public static void closeConnection(Connection conn) throws Exception { if ( conn != null ) { conn.close(); } }
public static int executeUpdate(String sql) throws Exception { int count = 0; Connection conn = null; Statement stmt = null; try { conn = openConnection(); stmt = conn.createStatement(); count = stmt.executeUpdate(sql); } catch ( Exception e ) { throw e; } finally { closeConnection(conn); } return count; } public static List executeQuery(String sql) throws Exception { List list = new ArrayList(); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = openConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); ResultSetMetaData rsmd = rs.getMetaData(); while ( rs.next() ) { Map map = new HashMap(); for ( int i = 1; i <= rsmd.getColumnCount(); i++ ) { map.put(rsmd.getColumnName(i), rs.getObject(i)); } list.add(map); }
} catch ( Exception e ) { e.printStackTrace(); } finally { if ( rs != null ) rs.close(); closeConnection(conn); } return list;
} }
</td> </tr> </table>

使用示例:
<table width="610" border=1 style="WIDTH: 513px; HEIGHT: 403px"> <tr> <td width="600">
// 1.对于insert, update, delete语句
int count = DbWrapper.executeUpdate(sql);
// 2.对于selete语句 java.util.List list = DbWrapper.executeQuery(sql);
// 方法一:按名字取值,注意大小写是严格区分的 for ( int i = 0; i < list.size(); i++ ) { java.util.Map map = (java.util.Map)list.get(i); out.println(mag.get("column_name").toString()); }
// 方法二:遍历取值 for ( int i = 0; i < list.size(); i++ ) { java.util.Map map = (java.util.Map)list.get(i);
for (java.util.Iterator it = map.keySet().iterator(); it.hasNext();) { String column_name = it.next().toString());

// 取值时注意null判断 out.println(column_name + " = " + map.get(column_name) == null ? "" :
map.get(column_name).toString()); } }
</td> </tr> </table>

                        
</td> </tr> </table> </td> </tr> <tr>


↑返回目录
前一篇: 我为什么憎恨Framework
后一篇: 根据图片的url将图片保存到本地