import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;import java.util.logging.Logger;
import javax.sql.rowset.CachedRowSet;import com.sun.rowset.CachedRowSetImpl;
public class RowSetFactory{ private static Logger logger=Logger.getLogger(RowSetFactory.class.getName()); static { try { //1)非连接池情况下,用这个 //Class.forName("com.mysql.jdbc.Driver"); //2)proxool连接池 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); }catch(Exception e) { logger.severe(e.getLocalizedMessage()); } } public static CachedRowSetImpl getRowSet() { try { //1)非连接池情况下,用这个 //connection不能够释放 CachedRowSetImpl rowSet=new CachedRowSetImpl(); rowSet.setUrl("jdbc:mysql://172.18.6.7:3306/wapchannel?useUnicode=true&characterEncoding=gb2312"); rowSet.setUsername("root"); rowSet.setPassword(""); //2)proxool连接池情况下,用这个 //用这个方法,默认的15个connectionCount很快就用完了 //rowSet.setUrl("proxool.test:com.mysql.jdbc.Driver:jdbc:mysql://172.18.6.7:3306/wapchannel?user=root&password=;&useUnicode=true&characterEncoding=gb2312"); rowSet.setCommand("select Fshortening,Ftitle,Fparent where Fen_topic=''"); rowSet.execute(); return rowSet; } catch(Exception e) { logger.severe("error happen when connect to the database 16.27"); return null; } } /** * 正确的使用方法 * @param sql * @return */ public static CachedRowSet query(String sql){ Connection conn = null; Statement stmt = null; ResultSet rset = null; try { CachedRowSetImpl rs= new CachedRowSetImpl(); String url = "proxool.test:com.mysql.jdbc.Driver:jdbc:mysql://172.23.2.3:3306/wapchannel?user=root&password=;&useUnicode=true&characterEncoding=gb2312"; Properties info= new Properties(); info.setProperty("proxool.maximum-connection-count", "300"); conn=DriverManager.getConnection(url,info); stmt = conn.createStatement(); rset = stmt.executeQuery(sql); rs.populate(rset); return rs; } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { conn.close(); } catch (Exception e) { } conn = null; stmt = null; rset = null; } return null; }
}