欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java數(shù)據(jù)庫(kù)操作類演示實(shí)例分享(java連接數(shù)據(jù)庫(kù))

 更新時(shí)間:2013年12月31日 09:35:37   作者:  
java數(shù)據(jù)庫(kù)操作類演示實(shí)例分享,大家參考使用吧

復(fù)制代碼 代碼如下:

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();
//  List<Map<String, Object>> userList = db.findAll("*");  // 查詢?nèi)?BR>//  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();
  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");          // 連接數(shù)據(jù)庫(kù)并設(shè)置編碼
  return DB.init("users");         // 設(shè)置操作的表名,并返回?cái)?shù)據(jù)庫(kù)操作對(duì)象
 }
}

相關(guān)文章

  • Java this關(guān)鍵字的使用詳解

    Java this關(guān)鍵字的使用詳解

    this 關(guān)鍵字是 Java 常用的關(guān)鍵字,可用于任何實(shí)例方法內(nèi)指向當(dāng)前對(duì)象,也可指向?qū)ζ湔{(diào)用當(dāng)前方法的對(duì)象,或者在需要當(dāng)前類型對(duì)象引用時(shí)使用
    2021-11-11
  • 基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項(xiàng)

    基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項(xiàng)

    這篇文章主要介紹了基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 關(guān)于Spring MVC框架中攔截器Interceptor的使用解讀

    關(guān)于Spring MVC框架中攔截器Interceptor的使用解讀

    這篇文章主要介紹了關(guān)于Spring MVC框架中攔截器Interceptor的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 使用java8 API遍歷過濾文件目錄及子目錄和隱藏文件示例詳解

    使用java8 API遍歷過濾文件目錄及子目錄和隱藏文件示例詳解

    這篇文章主要介紹了使用java8API遍歷過濾文件目錄及子目錄及隱藏文件示例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 簡(jiǎn)單了解JAVA SimpleDateFormat yyyy和YYYY的區(qū)別

    簡(jiǎn)單了解JAVA SimpleDateFormat yyyy和YYYY的區(qū)別

    這篇文章主要介紹了簡(jiǎn)單了解JAVA SimpleDateFormat yyyy和YYYY的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 最新評(píng)論