栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java数据库操作类演示实例分享(java连接数据库)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java数据库操作类演示实例分享(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();
//  List> 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> 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 map = db.where(new ContentValues().put("id", req.getParameter("id"))).find(
//    "id", "name", "birthday", "pwd");

  db.find(new DB.QueryCallback() {
   public void callback(Map 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");          // 连接数据库并设置编码
  return DB.init("users");         // 设置操作的表名,并返回数据库操作对象
 }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号