將druid和dbutils集成在一起,dbutils示例
使用Hibernate的時候覺得它太笨重了,而且還要在學一下HQL,可能不適合一些敏捷項目,小項目.然后就找到了dbutils,覺得還不錯.,因此嘗試著把druid和dbutils集成在一起.
首先創建一個DBUtilsHelper,用于連接數據庫,起到druid和dbutils之間的橋梁作用.
import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.dbutils.QueryRunner;public class DBUtilsHelper { private DataSource ds = null; private QueryRunner runner = null;
public DBUtilsHelper() { try { this.ds = DbPoolConnection.getInstance().getDataSource(); } catch (SQLException e) { e.printStackTrace(); } if (this.ds != null) { this.runner = new QueryRunner(this.ds); } } public DBUtilsHelper(DataSource ds) { this.ds = ds; this.runner = new QueryRunner(this.ds); } public QueryRunner getRunner() { return this.runner; }
}</pre>
再建一個druid初始化類
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import java.util.Properties; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.druid.pool.DruidPooledConnection; public class DbPoolConnection { private static DbPoolConnection databasePool = null; private static DruidDataSource dds = null; static { Properties properties = loadPropertyFile("db_server.properties"); try { dds = (DruidDataSource) DruidDataSourceFactory .createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } private DbPoolConnection() { } public static synchronized DbPoolConnection getInstance() { if (null == databasePool) { databasePool = new DbPoolConnection(); } return databasePool; } public DruidDataSource getDataSource() throws SQLException { return dds; } public DruidPooledConnection getConnection() throws SQLException { return dds.getConnection(); } public static Properties loadPropertyFile(String fullFile) { String webRootPath = null; if (null == fullFile || fullFile.equals("")) throw new IllegalArgumentException( "Properties file path can not be null : " + fullFile); webRootPath = DbPoolConnection.class.getClassLoader().getResource("\\") .getPath(); webRootPath = new File(webRootPath).getParent(); InputStream inputStream = null; Properties p = null; try { String profilepath = webRootPath + File.separator + fullFile; System.out.println(profilepath); inputStream = new FileInputStream(new File(profilepath)); p = new Properties(); p.load(inputStream); } catch (FileNotFoundException e) { throw new IllegalArgumentException("Properties file not found: " + fullFile); } catch (IOException e) { throw new IllegalArgumentException( "Properties file can not be loading: " + fullFile); } finally { try { if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return p; } }
下面是測試dbutils示例類
import java.sql.SQLException; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.KeyedHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; public class Druid_Dbutils_fully_test { public static void main(String[] args) throws SQLException { DBUtilsHelper dbh = new DBUtilsHelper(); QueryRunner runner = dbh.getRunner(); runner.update("insert into ssq(qishu) values('1883')"); runner.update("update ssq set qishu='1890' where qishu=?", "1880"); // 返回ArrayHandler結果,第一行結果:Object[] System.out.println("A:返回ArrayHandler結果......"); Object[] arrayResult = runner.query("select * from ssq", new ArrayHandler()); for (int i = 0; i < arrayResult.length; i++) { System.out.print(arrayResult[i] + " "); } System.out.println(); // 返回ArrayListHandler結果,第一行結果:List<Object[]> System.out.println("B:返回ArrayListHandler結果(僅顯示5行)........."); List<Object[]> arrayListResult = runner.query("select * from ssq", new ArrayListHandler()); for (int i = 0; i < arrayListResult.size() && i < 5; i++) { for (int j = 0; j < arrayListResult.get(i).length; j++) { System.out.print(arrayListResult.get(i)[j] + " "); } System.out.println(); } System.out.println(); // 返回bean System.out.println("X:單條返回bean結果."); ShuangSeQiu ssq = runner.query("select * from ssq where qishu like ?", new BeanHandler<ShuangSeQiu>(ShuangSeQiu.class), "2009%"); System.out.println("bean:" + ssq.getQishu()); System.out.println("X1:單條返回bean結果"); ResultSetHandler<ShuangSeQiu> h = new BeanHandler<ShuangSeQiu>( ShuangSeQiu.class); ShuangSeQiu p = runner.query( "select * from ssq where qishu like ? limit 1", h, "2009%"); System.out.println(p.getQishu()); // 返回beanlist System.out.println("C:返回BeanList結果(僅顯示5行)......"); List<ShuangSeQiu> beanListResult = runner.query("select * from ssq", new BeanListHandler<ShuangSeQiu>(ShuangSeQiu.class)); Iterator<ShuangSeQiu> iter_beanList = beanListResult.iterator(); int shownum = 0; while (iter_beanList.hasNext() && shownum < 5) { System.out.println(iter_beanList.next().getQishu()); shownum++; } // 返回指定列 System.out.println("D:返回ColumnList結果......"); List<Object> columnResult = runner.query("select * from ssq", new ColumnListHandler<Object>("qishu")); Iterator<Object> iter = columnResult.iterator(); shownum = 0; while (iter.hasNext() && shownum < 5) { System.out.println(iter.next()); shownum++; } // 返回KeyedHandler結果:Map<Object,Map<String,Object>>:map的key為KeyedHandler指定 System.out.println("E:返回KeyedHandler結果,期數:2003001的a列值........."); Map<Object, Map<String, Object>> keyedResult = runner.query( "select * from ssq", new KeyedHandler<Object>("qishu")); System.out.println(keyedResult.get("2003001").get("a")); // MapHandler System.out.println("F:返回MapHandler結果........."); Map<String, Object> mapResult = runner.query("select * from ssq", new MapHandler()); Iterator<String> iter_mapResult = mapResult.keySet().iterator(); while (iter_mapResult.hasNext()) { System.out.print(mapResult.get(iter_mapResult.next()) + " "); } System.out.println(); // 返回MapListHandler結果 System.out.println("G:返回MapListHandler結果........."); List<Map<String, Object>> mapListResult = runner.query( "select * from ssq", new MapListHandler()); for (int i = 0; i < mapListResult.size() && i < 5; i++) { Iterator<String> values = mapListResult.get(i).keySet().iterator(); while (values.hasNext()) { System.out.print(mapListResult.get(i).get(values.next()) + " "); } System.out.println(); } Object increaseId = runner.query("select last_insert_id()", new ScalarHandler<Object>()); System.out.println(increaseId); } }
貼了一堆代碼,就這么樣吧.
另外忘記貼druid配置了
下面列出來(db_server.properties)
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/shuangseqiu username=root password= filters=stat initialSize=2 maxActive=300 maxWait=60000 timeBetweenEvictionRunsMillis=60000 minEvictableIdleTimeMillis=300000 validationQuery=SELECT 1 testWhileIdle=true testOnBorrow=false testOnReturn=false poolPreparedStatements=false maxPoolPreparedStatementPerConnectionSize=200來自:http://my.oschina.net/muchuanwazi/blog/225127
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!