正在装载数据…… package org.lzh.struts; import javax.sql.DataSource; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; public class DB { /**connect*/ Connection connect = null; /**结果集*/ ResultSet rs = null; public DB(DataSource dataSource) { try { /*通过数据源,初始化连接池*/ connect = dataSource.getConnection(); } catch(SQLException e) { e.printStackTrace(); } } public ResultSet OpenSql(String sql) { try { /** * ResultSet.TYPE_SCROLL_INSENSITIVE * The constant indicating the type for a ResultSet object * that is scrollable but generally not sensitive to changes made by others. */ /** * ResultSet.CONCUR_READ_ONLY * The constant indicating the concurrency mode for a ResultSet object that may NOT be updated. */ Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql); } catch(SQLException ex) { ex.printStackTrace(); } return rs; } public int ExecSql(String sql) { int result = 0; try { Statement stmt = connect.createStatement(); result = stmt.executeUpdate(sql); } catch(SQLException ex) { System.err.println(ex.getMessage()); } return result; } /** * 关闭数据库连接池 * */ public void close(){ if(connect!=null){ try{ connect.close(); connect = null; }catch(SQLException ex) { System.err.println(ex.getMessage()); } } } } //数据库连接池的配置 <DATA-SOURCES> <DATA-SOURCE type="org.apache.commons.dbcp.BasicDataSource" key="dataSource"> <SET-PROPERTY value="root" property="password" /> <SET-PROPERTY value="3" property="minCount" /> <SET-PROPERTY value="10" property="maxCount" /> <SET-PROPERTY value="root" property="username" /> <SET-PROPERTY value="com.mysql.jdbc.Driver" property="driverClassName" /> <SET-PROPERTY value="strutsbbs" property="description" /> <SET-PROPERTY value="jdbc:mysql://localhost:3306/strutsbbs" property="url" /> <SET-PROPERTY value="false" property="readOnly" /> <SET-PROPERTY value="true" property="autoCommit" /> </DATA-SOURCE> </DATA-SOURCES> 本文来源:http://blog.csdn.net/nickshen3/archive/2007/06/17/1655826.aspx
|