兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          我們都知道一個數據系統的核心就是JDBC的編程,對數據進行操作,主要包括添加,刪除,更新以及查詢。
          在對數據進行操作之前首先要做的是:
             
              1.加載jdbc驅動程序;
              2.建立到指定數據庫的連接(連接池/數據源);
              3.提交數據庫操作命令;
              4.取得結果。

          下面看一下BlogServlet中關于add方法中的 code5-1
           1
           1 private void add(HttpServletRequest request, HttpServletResponse response)
           2             throws ServletException, IOException{
           3         response.setContentType("text/html;charset=UTF-8");
           4         request.setCharacterEncoding("UTF-8");
           5 
           6         String title = request.getParameter("title");
           7         String content = request.getParameter("content");
           8         String categoryId = request.getParameter("category");
           9 
          10         String sql =  "insert into blog(title,content,category_id,date) values(?,?,?,now())";
          11         String params[] = {title,content,categoryId};
          12         QueryRunner qr = DbHelper.getQueryRunner();
          13         int result = 0;
          14 
          15         try {
          16           result = qr.update(sql, params);
          17         } catch (SQLException ex) {
          18             Logger.getLogger(BlogServlet.class.getName()).log(Level.SEVERE, null, ex);
          19         }
          20     }

          在后面的文章中我們會提到apache提供的commons-dbutils-1.2jar有點小問題,這個我們以后還會提到。

          由于每次對jdbc編程少不了建立數據源,獲取數據源,建立連接的工作,所以這里再提供一個輔助類DbHelper來完成以上工作。
          DbHelper.java (code 5-2)
           1 package com.blog.utils;
           2 
           3 import java.util.logging.Level;
           4 import java.util.logging.Logger;
           5 import javax.naming.Context;
           6 import javax.naming.InitialContext;
           7 import javax.naming.NamingException;
           8 import javax.sql.DataSource;
           9 import org.apache.commons.dbutils.QueryRunner;
          10 
          11 /**
          12  *
          13  * @author Chucky
          14  */
          15 public class DbHelper {
          16 
          17     public static QueryRunner getQueryRunner() {
          18         DataSource ds = null;
          19         Context context = null;
          20         try {
          21             context = new InitialContext();
          22             ds = (DataSource) context.lookup("jdbc/Blog");
          23         } catch (NamingException ex) {
          24             Logger.getLogger(DbHelper.class.getName()).log(Level.SEVERE, null, ex);
          25         }
          26         QueryRunner qr = new QueryRunner(ds);
          27         return qr;
          28     }
          29 }

          現在通過DbUtils庫和DbHelper輔助類的使用,原先code 5-1可以簡化成
           1 private void add(HttpServletRequest request, HttpServletResponse response)
           2             throws ServletException, IOException{
           3         response.setContentType("text/html;charset=UTF-8");
           4         request.setCharacterEncoding("UTF-8");
           5 
           6         String title = request.getParameter("title");
           7         String content = request.getParameter("content");
           8         String categoryId = request.getParameter("category");
           9 
          10         String sql =  "insert into blog(title,content,category_id,date) values(?,?,?,now())";
          11         String params[] = {title,content,categoryId};
          12         QueryRunner qr = DbHelper.getQueryRunner();
          13         int result = 0;
          14 
          15         try {
          16           result = qr.update(sql, params);
          17         } catch (SQLException ex) {
          18             Logger.getLogger(BlogServlet.class.getName()).log(Level.SEVERE, null, ex);
          19         }
          20     }
          QueryRunner類是DbUtils的核心類,只要通過query()方法對數據查詢或update()對數據刪除delete/添加insert/更新update;
          在后面的文章中,會詳細解釋。

          posted on 2009-09-28 16:24 Chucky 閱讀(517) 評論(2)  編輯  收藏

          評論

          # re: Blog系統開發 5. JDBC的基本操作與DbUtils的使用 2009-12-22 03:20 冰城浪子
          學習的是v512的博客項目,和你的代碼一樣 就是在這出現錯誤, QueryRunner qr = DbHelper.getQueryRunner();
          我在MyEclipse下開發的,總是有問題
          在Netbeans下使用時 沒有一點的錯誤,請指教一下 ,謝謝了
          調了半天也沒有弄好,急!!!!!!!!!  回復  更多評論
            

          # re: Blog系統開發 5. JDBC的基本操作與DbUtils的使用 2009-12-22 17:28 Chucky
          出現什么錯誤呢? 報錯的信息是什么呢?
            回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 探索| 财经| 蓬安县| 潍坊市| 舟山市| 科技| 昌邑市| 饶阳县| 汕头市| 西吉县| 武宁县| 剑阁县| 瓮安县| 称多县| 和顺县| 盐山县| 彰武县| 通州市| 双城市| 永定县| 南平市| 万载县| 闽侯县| 清苑县| 金阳县| 五河县| 宜兰市| 宣恩县| 辽阳市| 隆子县| 湘潭市| 江源县| 百色市| 定兴县| 滨州市| 定结县| 芷江| 石楼县| 涡阳县| 博罗县| 丹寨县|