QueryRunner的基本用法

來源:互聯網
上載者:User

個簡化JDBC操作的組件——DBUtils,需要我們自己下載相應的jar包

DbUtils類(org.apache.commons.dbutils.DbUtils)主要負責裝載驅動、關閉串連的常規工作。

1.close: 檢查所提供的參數是不是NULL,如果不是的話,它們就關閉串連、聲明和結果集。

2.CloseQuietly:避免串連、聲明或結果集為NULL的情況被關閉。

3.CommitAndCloseQuietly(Connection conn):用來提交串連,然後關閉串連,並且在關閉串連時不向上拋出在關閉時發生的一些SQL異常。

4.LoadDriver(String driveClassName): 裝載並註冊JDBC驅動程式,如果成功就返回TRUE。

QreryRunner類(org.apache.commons.dbutils.QueryRunner) 顯著的簡化了SQL查詢,並與ResultSetHandler協同工作將使編碼量大為減少。

1.query(Connection conn, String sql, Object[] params, ResultSetHandler rsh):執行選取查詢,在查詢中,對象陣列的值被用來作為查詢的置換參數。

2.query(String sql, Object[] params, ResultSetHandler rsh):方法本身不提供資料庫連接,執行選取查詢,在查詢中,對象陣列的值被用來作為查詢的置換參數。

3.query(Connection conn, String sql, ResultSetHandler rsh):執行無需參數的選取查詢。

4.update(Connection conn, String sql, Object[] params):被用來執行插入、更新或刪除(DML)操作。

ResultSetHandler介面(org.apache.commons.dbutils.ResultSethandler)執行處理一個結果集對象,將資料轉變並處理為任何一種形式,供其他應用使用。

1. Object handle (Java.sql.ResultSet .rs) :結果集(ResultSet)作為參數傳入方法內,處理這個結果集,返回一個對象。

ArrayHandler

ArrayListHandler

BeanHandler

BeanListHandler

MapHandler

MapListHandler

ScalarHandler

1:首先要建一個jdbc類來提供資料來源:

public class JdbcUtil {
private static ComboPooledDataSource dataSource =new ComboPooledDataSource();
public static ComboPooledDataSource getDataSource()
{
return dataSource;
}

}:

2.src目錄下c3p0-config.xml,用來讀取資料庫的設定檔

 

3.基本資料庫操作

從資料庫中取count(*)資料

1         int topicNum=0;2         QueryRunner runner= new QueryRunner(JdbcUtil.getDataSource());3         String sql ="select count(*) from topic where type_id= ? order by time desc";4         Object[] params={typeId};5         topicNum=(int)(long) runner.query(sql,new ScalarHandler(),params);6         return topicNum;

 

取一條資料   其中表的欄位名字和類的名字要對應相同

1         Topic newlyTopic=null;2         QueryRunner runner= new QueryRunner(JdbcUtil.getDataSource());3         String sql ="select * from topic where type_id= ? order by time desc";4         Object[] params={typeId};5         newlyTopic= runner.query(sql,new BeanHandler<Topic>(Topic.class),params);6         return newlyTopic;

 

取集合資料List<>返回    其中表的欄位名字和類的名字要對應相同

1         List<Topic> topicList=new ArrayList<Topic>();2         QueryRunner runner= new QueryRunner(JdbcUtil.getDataSource());3         String sql ="select * from topic where type_id= ? order by time desc";4         Object[] params={typeId};5         topicList=runner.query(sql, new BeanListHandler<Topic>(Topic.class),params);6         return topicList;

 插入

 1         QueryRunner runner= new QueryRunner(JdbcUtil.getDataSource()); 2         String sql ="insert into topic(name,author,content,time,type_id) values(?,?,?,?,?)"; 3         Object[] params={topic.getName(),topic.getAuthor(),topic.getContent(),topic.getTime(),topic.getTypeId()}; 4         try { 5             //事務開始 6             runner.update(sql,params); 7             //事務提交 8         } catch (SQLException e) { 9             e.printStackTrace();10             //交易回復11             throw e;12         }
更新

 1         QueryRunner runner= new QueryRunner(JdbcUtil.getDataSource()); 2         String sql ="update topic set name=? , content=? , time=? where id= ?"; 3         Object[] params={topic.getName(),topic.getContent(),topic.getTime(),topic.getId()}; 4         try { 5             //事務開始 6             runner.update(sql,params); 7             //事務提交 8         } catch (SQLException e) { 9             e.printStackTrace();10             //交易回復11             throw e;12         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.