java數(shù)據(jù)庫操作類演示實(shí)例分享(java連接數(shù)據(jù)庫)
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("*"); // 查詢?nèi)?BR>// 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"); // 連接數(shù)據(jù)庫并設(shè)置編碼
return DB.init("users"); // 設(shè)置操作的表名,并返回?cái)?shù)據(jù)庫操作對象
}
}
- java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
- java使用jdbc操作數(shù)據(jù)庫示例分享
- java自定義動(dòng)態(tài)鏈接數(shù)據(jù)庫示例
- java連接orcale數(shù)據(jù)庫示例分享
- JAVA簡單鏈接Oracle數(shù)據(jù)庫 注冊和登陸功能的實(shí)現(xiàn)代碼
- java配置dbcp連接池(數(shù)據(jù)庫連接池)示例分享
- 通過代理類實(shí)現(xiàn)java連接數(shù)據(jù)庫(使用dao層操作數(shù)據(jù))實(shí)例分享
- 使用JAVA實(shí)現(xiàn)高并發(fā)無鎖數(shù)據(jù)庫操作步驟分享
- 將json當(dāng)數(shù)據(jù)庫一樣操作的javascript lib
- java連接MySQl數(shù)據(jù)庫實(shí)例代碼
- 通過java備份恢復(fù)mysql數(shù)據(jù)庫的實(shí)現(xiàn)代碼
- java連接mysql數(shù)據(jù)庫詳細(xì)步驟解析
- 淺析JAVA常用JDBC連接數(shù)據(jù)庫的方法總結(jié)
- Java實(shí)現(xiàn)獲得MySQL數(shù)據(jù)庫中所有表的記錄總數(shù)可行方法
- Java連接MYSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- java 獲取數(shù)據(jù)庫連接的實(shí)現(xiàn)代碼
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫的配置方法
- java實(shí)現(xiàn)數(shù)據(jù)庫主鍵生成示例
相關(guān)文章
Java利用future及時(shí)獲取多線程運(yùn)行結(jié)果
在Java編程中,有時(shí)候會(huì)需要及時(shí)獲取線程的運(yùn)行結(jié)果,本文就通過一個(gè)相關(guān)實(shí)例向大家介紹Java利用future及時(shí)獲取線程運(yùn)行結(jié)果的方法,需要的朋友可以參考。2017-10-10基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項(xiàng)
這篇文章主要介紹了基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11關(guān)于Spring MVC框架中攔截器Interceptor的使用解讀
這篇文章主要介紹了關(guān)于Spring MVC框架中攔截器Interceptor的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07使用java8 API遍歷過濾文件目錄及子目錄和隱藏文件示例詳解
這篇文章主要介紹了使用java8API遍歷過濾文件目錄及子目錄及隱藏文件示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07簡單了解JAVA SimpleDateFormat yyyy和YYYY的區(qū)別
這篇文章主要介紹了簡單了解JAVA SimpleDateFormat yyyy和YYYY的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03