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

所属分类: 软件编程 / java 阅读数: 76
收藏 0 赞 0 分享

复制代码 代码如下:

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("*");  // 查询全部
//  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");          // 连接数据库并设置编码
  return DB.init("users");         // 设置操作的表名,并返回数据库操作对象
 }
}

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多