主頁(yè) > 知識(shí)庫(kù) > jsp+servlet實(shí)現(xiàn)最簡(jiǎn)單的增刪改查代碼分享

jsp+servlet實(shí)現(xiàn)最簡(jiǎn)單的增刪改查代碼分享

熱門標(biāo)簽:百度地圖添加標(biāo)注圖標(biāo)樣式 如何用中國(guó)地圖標(biāo)注數(shù)字點(diǎn) 南京新思維電話機(jī)器人 聊城智能電銷機(jī)器人外呼 地圖標(biāo)注市場(chǎng)怎么樣 企業(yè)怎么在聯(lián)通申請(qǐng)400電話 泰州泰興400電話 怎么申請(qǐng) 好操作的電話機(jī)器人廠家 南昌市地圖標(biāo)注app

話不多說,請(qǐng)看代碼

package ceet.ac.cn.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ceet.ac.cn.model.Admin;
public class AdminDao {
 public ListAdmin> getAllAdmin(){ //查詢所有信息
 ListAdmin> list = new ArrayListAdmin>(); //創(chuàng)建集合
 Connection conn = DbHelper.getConnection();
 String sql = "select * from admin"; //SQL查詢語(yǔ)句
 try {
  PreparedStatement pst = conn.prepareStatement(sql);
  ResultSet rst = pst.executeQuery();
  while (rst.next()) {
  Admin admin = new Admin();
  admin.setId(rst.getInt("id")); //得到ID
  admin.setUsername(rst.getString("username"));
  admin.setUserpwd(rst.getString("userpwd"));
  list.add(admin);
  }
  rst.close(); //關(guān)閉
  pst.close(); //關(guān)閉
 } catch (SQLException e) {
  e.printStackTrace(); //拋出異常
 }
 return list; //返回一個(gè)集合
 }
 public boolean addAdmin(Admin admin){ //添加信息
 String sql = "INSERT INTO `admin`(`id`,`username`,`userpwd`) VALUES (?,?,?)"; //添加的SQL語(yǔ)句
 Connection conn = DbHelper.getConnection();
 try {
  PreparedStatement pst = conn.prepareStatement(sql);
  pst.setInt(1, admin.getId());
  pst.setString(2, admin.getUsername());
  pst.setString(3, admin.getUserpwd());
  int count = pst.executeUpdate();
  pst.close();
  return count>0?true:false; //是否添加的判斷
 } catch (SQLException e) {
  e.printStackTrace();
 }
 return false;
 }
 public boolean updateAdmin(Admin admin){ //修改
 String sql = "UPDATE `admin` SET `username`=?,`userpwd`=? WHERE `id` = ?"; //修改的SQL語(yǔ)句,根據(jù)ID修改
 Connection conn = DbHelper.getConnection();
 try {
  PreparedStatement pst = conn.prepareStatement(sql);
  pst.setString(1, admin.getUsername());
  pst.setString(2, admin.getUserpwd());
  pst.setInt(3, admin.getId()); //根據(jù)的ID
  int count = pst.executeUpdate();
  pst.close(); //關(guān)閉
  return count>0?true:false; //是否修改的判斷
 } catch (SQLException e) {
  e.printStackTrace();
 }
 return false;
 }
 public boolean deleteAdmin(int id){ //刪除
 String sql = "delete from admin where id = ?"; //刪除的SQL語(yǔ)句,根據(jù)ID刪除
 Connection conn = DbHelper.getConnection();
 try {
  PreparedStatement pst = conn.prepareStatement(sql);
  pst.setInt(1, id);
  int count = pst.executeUpdate();
  pst.close();
  return count>0?true:false; //是否刪除的判斷
 } catch (SQLException e) {
  e.printStackTrace();
 }
 return false;
 }
 public Admin selectAdminById(int id){ //根據(jù)ID進(jìn)行查詢
 Connection conn = DbHelper.getConnection();
 String sql = "select * from admin where id = "+id;
 Admin admin = null;
 try {
  PreparedStatement pst = conn.prepareStatement(sql);
  ResultSet rst = pst.executeQuery();
  while (rst.next()) {
  admin = new Admin();
  admin.setId(rst.getInt("id"));
  admin.setUsername(rst.getString("username"));
  admin.setUserpwd(rst.getString("userpwd"));
  }
  rst.close();
  pst.close();
 } catch (SQLException e) {
  e.printStackTrace();
 }
 return admin; //返回
 }
}
package ceet.ac.cn.dao;
import java.sql.Connection;
import java.sql.DriverManager;
/**
 * 連接數(shù)據(jù)庫(kù)
 * @author 畫船聽雨眠
 *
 */
public class DbHelper {
 private static String url = "jdbc:mysql://localhost:3306/admin"; //數(shù)據(jù)庫(kù)地址
 private static String userName = "root"; //數(shù)據(jù)庫(kù)用戶名
 private static String passWord = "359129127"; //數(shù)據(jù)庫(kù)密碼
 private static Connection conn = null;
 private DbHelper(){
 }
 public static Connection getConnection(){
 if(null == conn){
  try {
  Class.forName("com.mysql.jdbc.Driver");
  conn = DriverManager.getConnection(url, userName, passWord);
  } catch (Exception e) {
  e.printStackTrace();
  }
 }
 return conn;
 }
 public static void main(String[] args) { //測(cè)試數(shù)據(jù)庫(kù)是否連通
 System.err.println(getConnection());
 }
}
package ceet.ac.cn.model;
import java.io.Serializable;
public class Admin implements Serializable{ //數(shù)據(jù)封裝類
 private static final long serialVersionUID = 1L;
 private int id;
 private String username;
 private String userpwd;
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getUsername() {
 return username;
 }
 public void setUsername(String username) {
 this.username = username;
 }
 public String getUserpwd() {
 return userpwd;
 }
 public void setUserpwd(String userpwd) {
 this.userpwd = userpwd;
 }
}
package ceet.ac.cn.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ceet.ac.cn.dao.AdminDao;
import ceet.ac.cn.model.Admin;
public class AddServlet extends HttpServlet{ //添加數(shù)據(jù)
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 this.doPost(req, resp);
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 String username = req.getParameter("username");
 String userpwd = req.getParameter("userpwd");
 Admin admin = new Admin();
 admin.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8")); //轉(zhuǎn)值,中文需要轉(zhuǎn)換為utf-8
 admin.setUserpwd(new String(userpwd.getBytes("ISO-8859-1"),"UTF-8"));
 AdminDao dao = new AdminDao();
 dao.addAdmin(admin);
 req.getRequestDispatcher("ShowServlet").forward(req, resp);
 } 
}
package ceet.ac.cn.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ceet.ac.cn.dao.AdminDao;
public class DeleteServlet extends HttpServlet{ //刪除數(shù)據(jù)
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 this.doPost(req, resp);
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 String idStr = req.getParameter("id"); //刪除數(shù)據(jù)的ID,根據(jù)ID刪除
 if(idStr != null  !idStr.equals("")){
  int id = Integer.valueOf(idStr);
  AdminDao dao = new AdminDao();
  dao.deleteAdmin(id);
 }
 req.getRequestDispatcher("ShowServlet").forward(req, resp);
 }
}
package ceet.ac.cn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ceet.ac.cn.dao.AdminDao;
import ceet.ac.cn.model.Admin;
public class ShowServlet extends HttpServlet{ //顯示全部數(shù)據(jù)
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 this.doPost(req, resp);
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 AdminDao dao = new AdminDao();
 ListAdmin> list = dao.getAllAdmin();
 req.setAttribute("list", list);
 req.getRequestDispatcher("index.jsp").forward(req, resp);
 } 
}
package ceet.ac.cn.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ceet.ac.cn.dao.AdminDao;
import ceet.ac.cn.model.Admin;
public class UpdateServlet extends HttpServlet{ //修改
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException { //查詢到選中ID的值所對(duì)應(yīng)的數(shù)據(jù)
 String idStr = req.getParameter("id");
 if(idStr != null  !idStr.equals("")){
  int id = Integer.valueOf(idStr);
  AdminDao dao = new AdminDao();
  Admin admin = dao.selectAdminById(id);
  req.setAttribute("admin", admin);
 }
 req.getRequestDispatcher("update.jsp").forward(req, resp);

 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException { //根據(jù)此ID對(duì)數(shù)據(jù)的值進(jìn)行修改
 String username = req.getParameter("username");
 String userpwd = req.getParameter("userpwd");
 String idStr = req.getParameter("id");
 Admin admin = new Admin();
 admin.setId(Integer.valueOf(idStr));
 admin.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
 admin.setUserpwd(new String(userpwd.getBytes("ISO-8859-1"),"UTF-8"));
 AdminDao dao = new AdminDao();
 dao.updateAdmin(admin);
 req.getRequestDispatcher("ShowServlet").forward(req, resp);
 }
}
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
 head> 
 title>添加/title>
 link rel="stylesheet" href="css/index.css" type="text/css" />
 /head>
 body>
 form action="AddServlet" method="post">
 table border="1" class="t1">
 tr>
  td colspan="2">h1>添加管理員/h1>/td>
 /tr>
 tr>
  td>管理員帳號(hào):/td>
  td>input type="text" name="username"/>/td>
 /tr>
 tr>
  td>管理員密碼:/td>
  td>input type="password" name="userpwd"/>/td>
 /tr>
 tr>
  td colspan="2">
  input type="submit" value="提交"/>
  input type="reset" value="清空"/>
  /td>
 /tr>
 /table>
 /form>
 /body>
/html>
%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
html>
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8">
title>顯示/title>
 style type="text/css">
  table {
  border: 1px solid pink;
  margin: 0 auto;
  }
  td{
  width: 150px;
  border: 1px solid pink;
  text-align: center;
  }
 /style>
/head>
body>
 table>
 tr>
  td>編號(hào)/td>
  td>帳號(hào)/td>
  td>密碼/td>
  td>操作/td>
 /tr>
 c:forEach items="${list}" var="item">
  tr>
  td>${item.id }/td>
  td>${item.username }/td>
  td>${item.userpwd }/td>
  td>a href="DeleteServlet?id=${item.id }">刪除/a>|a href="UpdateServlet?id=${item.id }">修改/a>/td>
  /tr>
 /c:forEach>
 tr>
  td colspan="6" style="text-align: left;">a href="add.jsp">添加管理員/a>/td>
 /tr>
 /table>
/body>
/html>
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
 head> 
 title>修改/title>
 link rel="stylesheet" href="css/index.css" type="text/css" />
 /head>
 body>
 form action="UpdateServlet" method="post">
 table border="1" class="t1">
 tr>
  td colspan="2">h1>修改管理員信息/h1>/td>
 /tr>
 tr>
  td>編號(hào):/td>
  td>input type="text" name="id" value="${admin.id}" readonly="readonly"/>/td>
 /tr>
 tr>
  td>管理員帳號(hào):/td>
  td>input type="text" name="username" value="${admin.username}"/>/td>
 /tr>
 tr>
  td>管理員密碼:/td>
  td>input type="text" name="userpwd" value="${admin.userpwd}"/>/td>
 /tr>
 tr>
  td colspan="2">
  input type="submit" value="提交"/>
  input type="button" value="返回" onclick="history.go(-1)"/>
  /td>
 /tr>
 /table>
 /form>
 /body>
/html>
@CHARSET "UTF-8";
 table.t1 {
  margin-top:10px;
  margin-left:20px;
  margin-right:20px;
  margin-bottom:5px;
  #background-color: #FFF;
  #background:#EEF4F9;
  #border: none;
  border: 1;
  #color:#003755;
  border-collapse:collapse;
  font: 14px "宋體";
  text-align: center;
 }
 table.t1 th{
  background:#7CB8E2;
  color:#fff;
  padding:6px 4px;
  text-align:center;
 }
 table.t1 td{
  background:#C7DDEE none repeat-x scroll center left;
  color:#000;
  padding:4px 2px;
 }
 table.t1 a{
  text-decoration:none;
  height:1em;
 }
 table.t1 a:link, table.t1 a:visited{
  color:#3366CC;
 }
 table.t1 a:hover{
  color:#B50000;
  text-decoration:underline;
 }

最簡(jiǎn)單的jsp+servlet的增刪改查代碼。寫的很清楚,就這樣了。

實(shí)現(xiàn)原理:

每行數(shù)據(jù)后面加一個(gè)編輯和刪除按鈕,按鈕提交到后臺(tái)并且?guī)в写诵袛?shù)據(jù)的主要參數(shù)。

點(diǎn)擊編輯按鈕,通過servlet操作jsp將此行的每一列替換為一個(gè)文本框并把已有的值帶進(jìn)去,后面一個(gè)提交按鈕通過submit提交數(shù)據(jù)并將文本框重新變?yōu)楸砀竦膯卧瘛?/p>

新增,就像編輯一樣,添加一行,全部是文本框。。。

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

您可能感興趣的文章:
  • JSP 開發(fā)之Servlet解決網(wǎng)頁(yè)緩存問題
  • 淺談Servlet轉(zhuǎn)發(fā)到JSP頁(yè)面的路徑問題(必看)
  • JSP開發(fā)Servlet重寫init()方法實(shí)例詳解
  • JSP + Servlet實(shí)現(xiàn)生成登錄驗(yàn)證碼示例
  • jsp+servlet+jdbc實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查
  • jsp登陸校驗(yàn)演示 servlet、login、success
  • JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
  • Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
  • JSP中通過Servlet 將服務(wù)器硬盤圖片并展示到瀏覽器

標(biāo)簽:烏蘭察布 山南 白銀 自貢 銅川 吉林 開封 臨汾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp+servlet實(shí)現(xiàn)最簡(jiǎn)單的增刪改查代碼分享》,本文關(guān)鍵詞  jsp+servlet,實(shí)現(xiàn),最簡(jiǎn)單,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp+servlet實(shí)現(xiàn)最簡(jiǎn)單的增刪改查代碼分享》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于jsp+servlet實(shí)現(xiàn)最簡(jiǎn)單的增刪改查代碼分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章