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<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"); // 设置操作的表名,并返回数据库操作对象
}
}
上一篇: mysql主键的缺少导致备库hang住