时间:2021-05-19
复制代码 代码如下:
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"); // 设置操作的表名,并返回数据库操作对象
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Java实现JSP使用Servelt连接Oracle数据库的方法。为了实现在Servlet中连接数据库,必须编写Servlet的类程序。将Serv
本文实例讲述了Java使用JDBC连接数据库的实现方法,是Java数据库程序设计里非常实用的重要技巧。分享给大家供大家参考。具体如下:JDBC(JavaData
本文实例讲述了Java使用jdbc连接MySQL数据库的方法。分享给大家供大家参考,具体如下:使用jdbc连接数据库:可以直接在方法中定义url、user、ps
本文实例讲述了java开发中基于JDBC连接数据库的方法。分享给大家供大家参考,具体如下:创建一个以JDBC连接数据库的程序,包含7个步骤:1、加载JDBC驱动
本文实例为大家分享了java连接MySQL数据库代码,供大家参考,具体内容如下//java连接MySQl数据库实例代码packagecom.abc.dao;/*