java數(shù)據(jù)庫(kù)操作類(lèi)演示實(shí)例分享(java連接數(shù)據(jù)庫(kù))
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("*"); // 查詢(xún)?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用戶(hù)名
// DB.PWD = ""; // mysql密碼
DB.connect("utf-8"); // 連接數(shù)據(jù)庫(kù)并設(shè)置編碼
return DB.init("users"); // 設(shè)置操作的表名,并返回?cái)?shù)據(jù)庫(kù)操作對(duì)象
}
}
- java使用jdbc連接數(shù)據(jù)庫(kù)工具類(lèi)和jdbc連接mysql數(shù)據(jù)示例
- java使用jdbc操作數(shù)據(jù)庫(kù)示例分享
- java自定義動(dòng)態(tài)鏈接數(shù)據(jù)庫(kù)示例
- java連接orcale數(shù)據(jù)庫(kù)示例分享
- JAVA簡(jiǎn)單鏈接Oracle數(shù)據(jù)庫(kù) 注冊(cè)和登陸功能的實(shí)現(xiàn)代碼
- java配置dbcp連接池(數(shù)據(jù)庫(kù)連接池)示例分享
- 通過(guò)代理類(lèi)實(shí)現(xiàn)java連接數(shù)據(jù)庫(kù)(使用dao層操作數(shù)據(jù))實(shí)例分享
- 使用JAVA實(shí)現(xiàn)高并發(fā)無(wú)鎖數(shù)據(jù)庫(kù)操作步驟分享
- 將json當(dāng)數(shù)據(jù)庫(kù)一樣操作的javascript lib
- java連接MySQl數(shù)據(jù)庫(kù)實(shí)例代碼
- 通過(guò)java備份恢復(fù)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- java連接mysql數(shù)據(jù)庫(kù)詳細(xì)步驟解析
- 淺析JAVA常用JDBC連接數(shù)據(jù)庫(kù)的方法總結(jié)
- Java實(shí)現(xiàn)獲得MySQL數(shù)據(jù)庫(kù)中所有表的記錄總數(shù)可行方法
- Java連接MYSQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
- java 獲取數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)代碼
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫(kù)的配置方法
- java實(shí)現(xiàn)數(shù)據(jù)庫(kù)主鍵生成示例
相關(guān)文章
jenkins自動(dòng)構(gòu)建SpringCloud流程
文章介紹了如何使用Jenkins和docker-compose自動(dòng)部署SpringCloud和Vue項(xiàng)目,首先,下載并安裝Jenkins,并配置Jenkins以自動(dòng)啟動(dòng),然后,配置GitLab插件和NodeJS插件,以便可以從GitLab倉(cāng)庫(kù)中獲取代碼并構(gòu)建,接下來(lái),創(chuàng)建一個(gè)Jenkins任務(wù)來(lái)構(gòu)建Vue項(xiàng)目2025-02-02
Java 多線(xiàn)程有序執(zhí)行的幾種方法總結(jié)
這篇文章主要介紹了Java 多線(xiàn)程有序執(zhí)行的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Java隨機(jī)值設(shè)置(java.util.Random類(lèi)或Math.random方法)
在編程中有時(shí)我們需要生成一些隨機(jī)的字符串作為授權(quán)碼、驗(yàn)證碼等,以確保數(shù)據(jù)的安全性和唯一性,這篇文章主要給大家介紹了關(guān)于Java隨機(jī)值設(shè)置的相關(guān)資料,主要用的是java.util.Random類(lèi)或Math.random()方法,需要的朋友可以參考下2024-08-08
Java實(shí)現(xiàn)鏈表數(shù)據(jù)結(jié)構(gòu)的方法
這篇文章主要介紹了Java實(shí)現(xiàn)鏈表數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,每一個(gè)鏈表都包含多個(gè)節(jié)點(diǎn),節(jié)點(diǎn)又包含兩個(gè)部分,一個(gè)是數(shù)據(jù)域(儲(chǔ)存節(jié)點(diǎn)含有的信息),一個(gè)是引用域(儲(chǔ)存下一個(gè)節(jié)點(diǎn)或者上一個(gè)節(jié)點(diǎn)的地址),需要的朋友可以參考下2022-01-01
如何使用JDBC連接數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句
JDBC是Java數(shù)據(jù)庫(kù)連接的縮寫(xiě),是Java程序與數(shù)據(jù)庫(kù)進(jìn)行交互的標(biāo)準(zhǔn)API。JDBC主要包括Java.sql和javax.sql兩個(gè)包,通過(guò)DriverManager獲取數(shù)據(jù)庫(kù)連接對(duì)象Connection,并通過(guò)Statement或PreparedStatement執(zhí)行SQL語(yǔ)句2023-04-04
SpringBoot集成WebSocket【基于純H5】進(jìn)行點(diǎn)對(duì)點(diǎn)[一對(duì)一]和廣播[一對(duì)多]實(shí)時(shí)推送
這篇文章主要介紹了SpringBoot集成WebSocket【基于純H5】進(jìn)行點(diǎn)對(duì)點(diǎn)[一對(duì)一]和廣播[一對(duì)多]實(shí)時(shí)推送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

