Java資料庫操作類示範

來源:互聯網
上載者:User

標籤:http   java   os   io   資料   for   ar   cti   

只在mysql上測試過,不知道算不算好使
?1. [代碼][Java]代碼     
package org.load.demo;
 
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.loadphp.simple4j.ContentValues;
import com.loadphp.simple4j.DB;
import com.loadphp.simple4j.Utils;
 
public class MyServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String action = req.getParameter("action");
        if("show".equalsIgnoreCase(action)) {
            this.findAll(req, resp);
        }else if("del".equalsIgnoreCase(action)) {
            this.del(req, resp);
        }else if("edit".equalsIgnoreCase(action)) {
            this.find(req, resp);
        }else if("update".equalsIgnoreCase(action)) {
            this.update(req, resp);
        }else if("insert".equalsIgnoreCase(action)) {
            this.insert(req, resp);
        }
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doGet(req, resp);
    }
     
 
    private void findAll(final HttpServletRequest req, HttpServletResponse resp) {
//      DB db = this.getDB();flash歌曲
//      List<Map<String, Object>> userList = db.findAll("*");  // 查詢全部
//      db.close();
//      req.setAttribute("userList", userList);
//      try {
//          req.getRequestDispatcher("/index.jsp").forward(req, resp);
//      } catch (ServletException e) {
//          e.printStackTrace();
//      } catch (IOException e) {
//          e.printStackTrace();
//      }
         
        DB db = this.getDB();
        db.findAll(new DB.QueryAllCallback() {
            public void callback(List<Map<String, Object>> list) {
                req.setAttribute("userList", list);
            }
        }, "*");
         
        try {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    private void del(HttpServletRequest req, HttpServletResponse resp) {
        DB db = this.getDB();
        db.where(new ContentValues().put("id", req.getParameter("id"))).del();
        db.close();
        this.findAll(req, resp);
    }
     
    private void find(final HttpServletRequest req, HttpServletResponse resp) {
        DB db = this.getDB();
//      Map<String, Object> map = db.where(new ContentValues().put("id", req.getParameter("id"))).find(
//              "id", "name", "birthday", "pwd");
         
        db.find(new DB.QueryCallback() {
            public void callback(Map<String, Object> map) {
                req.setAttribute("user", map);
            }
        }, "id","name","birthday");
         
        db.close();
         
        try {
            req.getRequestDispatcher("/edit.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    private void insert(HttpServletRequest req, HttpServletResponse resp) {
        DB db = this.getDB();http://www.huiyi8.com/flashgequ/wangluo/
        db.insert(Utils.params2Array(req, 3, "null","user","birth","pwd"));
        db.close();
        this.findAll(req, resp);
    }
     
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        DB db = this.getDB();
        db.where(new ContentValues().put("id", req.getParameter("id"))).update(
                new ContentValues().put("name", req.getParameter("user"))
                        .put("pwd", Utils.md5(req.getParameter("pwd")))
                        .put("birthday", req.getParameter("birth")));
        db.close();
        this.findAll(req, resp);
    }
     
    private DB getDB() {
//      DB.DRIVER = "com.mysql.jdbc.Driver";               // driver
        DB.URI = "jdbc:mysql://localhost:3306/forjava";    // uri
//      DB.USER = "root";                                  // mysql使用者名稱
//      DB.PWD = "";                                       // mysql密碼
        DB.connect("utf-8");                               // 串連資料庫並設定編碼
        return DB.init("users");                           // 設定作業的表名,並返回資料庫操作對象
    }
}
?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.