Java Database Operations Class demo instance sharing (Java connection database) _java

Source: Internet
Author: User

Copy Code code as follows:

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 ("*"); Query all
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 ("IDs", 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 User name
Db.            PWD = ""; MySQL Password
Db.connect ("Utf-8"); Connect the database and set the encoding
Return Db.init ("users"); Sets the table name of the action and returns the database Action object
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.