话不多说,请看代码
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 list<admin> getalladmin(){ //查询所有信息
list<admin> list = new arraylist<admin>(); //创建集合
connection conn = dbhelper.getconnection();
string sql = "select * from admin"; //sql查询语句
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(); //关闭
pst.close(); //关闭
} catch (sqlexception e) {
e.printstacktrace(); //抛出异常
}
return list; //返回一个集合
}
public boolean addadmin(admin admin){ //添加信息
string sql = "insert into `admin`(`id`,`username`,`userpwd`) values (?,?,?)"; //添加的sql语句
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语句,根据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()); //根据的id
int count = pst.executeupdate();
pst.close(); //关闭
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语句,根据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){ //根据id进行查询
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;
/**
* 连接数据库
* @author 画船听雨眠
*
*/
public class dbhelper {
private static string url = "jdbc:mysql://localhost:3306/admin"; //数据库地址
private static string username = "root"; //数据库用户名
private static string password = "359129127"; //数据库密码
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) { //测试数据库是否连通
system.err.println(getconnection());
}
}
package ceet.ac.cn.model;
import java.io.serializable;
public class admin implements serializable{ //数据封装类
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{ //添加数据
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")); //转值,中文需要转换为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{ //删除数据
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"); //删除数据的id,根据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{ //显示全部数据
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();
list<admin> 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的值所对应的数据
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 { //根据此id对数据的值进行修改
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>管理员帐号:</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>编号</td>
<td>帐号</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>编号:</td>
<td><input type="text" name="id" value="${admin.id}" readonly="readonly"/></td>
</tr>
<tr>
<td>管理员帐号:</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;
}
最简单的jsp+servlet的增删改查代码。写的很清楚,就这样了。
实现原理:
每行数据后面加一个编辑和删除按钮,按钮提交到后台并且带有此行数据的主要参数。
点击编辑按钮,通过servlet操作jsp将此行的每一列替换为一个文本框并把已有的值带进去,后面一个提交按钮通过submit提交数据并将文本框重新变为表格的单元格。
新增,就像编辑一样,添加一行,全部是文本框。。。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持硕编程!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!