- 浏览: 187459 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
Errorize:
一个用servlet实现导出csv文件的实例 -
wendellhu:
求monkey talk应用文档的解压密码
monkey talk应用文档 -
wendellhu:
加密的?
monkey talk应用文档 -
hugang357:
hslh22 写道不错的资源,可以使用,只是没有最大最小值的限 ...
数字微调输入框 -
hslh22:
不错的资源,可以使用,只是没有最大最小值的限制,需要自己去加逻 ...
数字微调输入框
[align=center]工具:eclipse 3 数据库:mysql 5.0
自己写的一个CURD使用了jquery插件jquery-impromptu.2.7.min.js,
一个页面实现一张表的增删改查,无任何跳转,访问很快
项目结构如下:
test数据库,user表
sql:
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(12) default NULL,
`addr` varchar(12) default NULL,
PRIMARY KEY (`id`)
)
util包代码:
-------------------------------------------------------------------------
UtilTool .java
package com.test.util;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
/**
*
* @author zhaiyu
* @see 常用方法工具包
*/
public class UtilTool {
/*
* 验证字符串是否为空
*/
public static boolean isNotBlank(String temp) {
if (temp != null && !"".equals(temp)) {
return true;
} else {
return false;
}
}
/*
* 验证数字是否为空
*/
public static boolean isNotBlank(int temp) {
if (!"".equals(temp) && temp > 0) {
return true;
} else {
return false;
}
}
/*
* 验证数字是否有效
*/
public static boolean isNumAvaliable(int temp) {
if (!"".equals(temp) && temp > 0) {
return true;
} else {
return false;
}
}
}
DbConnection.java
package com.test.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbConnection {
public static Connection getConnection(){
Connection con=null;
String CLASSFORNAME="com.mysql.jdbc.Driver";
String SERVANDDB="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8";
String USER="root";
String PWD="admin";
try{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB,USER,PWD);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
public static void main(String args []){
System.out.println(DbConnection.getConnection());
}
}
PaginationNotice.java
package com.test.util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.test.model.User;
public class PaginationNotice {
private int allRows;
private int curPage = 1;
private int rowPerPage = 10;
private int allPages;
public int getRowPerPage() {
return rowPerPage;
}
public int getAllRows() {
return allRows;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getCurPage() {
return curPage;
}
public int getAllPages() {
return allPages;
}
public List<User> queryAll() {
List<User> list = new ArrayList<User>();
Statement stmt = null;
ResultSet rst = null;
try {
stmt = DbConnection.getConnection()
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rst = stmt.executeQuery("select * from user");
rst.last();
this.allRows = rst.getRow();
if (this.allRows % this.rowPerPage == 0) {
this.allPages = this.allRows / this.rowPerPage;
} else {
this.allPages = this.allRows / this.rowPerPage + 1;
}
if (this.curPage == 1) {
rst.beforeFirst();
} else {
rst.absolute((this.curPage - 1) * this.rowPerPage);
}
int i = 0;
while (rst.next() && i < this.rowPerPage) {
User user = new User();
user.setId(rst.getInt(1));
user.setUsername(rst.getString(2));
user.setAddr(rst.getString(3));
list.add(user);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
model包代码:
-------------------------------------------------------------------------
User.java
package com.test.model;
public class User {
private int id;
private String username;
private String addr;
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 getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
DAO包代码:
-------------------------------------------------------------------------
UserDAO.java
package com.test.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.test.model.User;
import com.test.util.DbConnection;
public class UserDAO {
private Connection con;
/** 通过id查询 */
public User queryById(String id) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
User user = null;
ResultSet rst = null;
try {
pstmt = con.prepareStatement("select * from user where id = ?");
pstmt.setString(1, id);
rst = pstmt.executeQuery();
while (rst.next()) {
user = new User();
user.setId(rst.getInt(1));
user.setUsername(rst.getString(2));
user.setAddr(rst.getString(3));
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
con.close();
}
return user;
}
/** 增加一条记录 */
public void addRecord(User user) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("insert into user (username,addr) values(?,?)");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getAddr());
pstmt.execute();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
con.close();
}
}
/** 删除一条记录 */
public void deleteRecord(int id) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("delete from user where id = ?");
pstmt.setInt(1, id);
pstmt.execute();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
con.close();
}
}
/** 更新一条记录 */
public void update(User user) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("update user set username=?,addr=? where id=?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getAddr());
pstmt.setInt(3, user.getId());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
con.close();
}
}
}
service->itf包代码:
-------------------------------------------------------------------------
package com.test.service.itf;
import com.test.model.User;
/**
* @author zhaiyu
* @see 用户公告表的业务逻辑接口
* @version 2009.8.25
*/
public interface IUserService {
/** 查询一条记录 */
public User getoneRecord(String id) throws Exception;
/** 增加一条记录 */
public void addRecord(User user) throws Exception;
/** 删除一条记录 */
public void deleteUserById(int id) throws Exception;
/** 更新一条记录 */
public void update(User user) throws Exception;
}
service->impl包代码:
-------------------------------------------------------------------------
package com.test.service.impl;
import com.test.dao.UserDAO;
import com.test.model.User;
import com.test.service.itf.IUserService;
public class UserServiceImpl implements IUserService {
private UserDAO dao = new UserDAO();
/** 增加一条记录 */
public void addRecord(User user) throws Exception {
dao.addRecord(user);
}
/** 查询一条记录 */
public User getoneRecord(String id) throws Exception {
return dao.queryById(id);
}
/** 删除一条记录 */
public void deleteUserById(int id) throws Exception {
dao.deleteRecord(id);
}
/** 更新一条记录 */
public void update(User user) throws Exception {
dao.update(user);
}
}
controller包代码:
-------------------------------------------------------------------------
UserServlet.java
package com.test.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.model.User;
import com.test.service.impl.UserServiceImpl;
import com.test.service.itf.IUserService;
import com.test.util.UtilTool;
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = -2446220534127375106L;
private IUserService userv = new UserServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("del".equals(action)) {
String id = request.getParameter("id");
if (UtilTool.isNotBlank(id)) {
try {
userv.deleteUserById(Integer.parseInt(id));
PrintWriter out = response.getWriter();
out.println("删除成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if ("add".equals(action)) {
String username = request.getParameter("username");
String addr = request.getParameter("addr");
if (UtilTool.isNotBlank(username)&&UtilTool.isNotBlank(addr)) {
try {
User user = new User();
user.setUsername(username);
user.setAddr(addr);
userv.addRecord(user);
PrintWriter out = response.getWriter();
out.println("添加成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if ("update".equals(action)) {
String id = request.getParameter("id");
String username = request.getParameter("username");
if (UtilTool.isNotBlank(username) && UtilTool.isNotBlank(id)) {
try {
User user = new User();
int uid = Integer.parseInt(id);
user.setId(uid);
user.setUsername(username);
userv.update(user);
PrintWriter out = response.getWriter();
out.println("公告修改成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if ("query".equals(action)) {
String id = request.getParameter("id");
if (UtilTool.isNotBlank(id)) {
try {
User user = userv.getoneRecord(id);
if (user != null) {
PrintWriter out = response.getWriter();
out
.println("username:"
+ user.getUsername()
+ "<br>"
+ "address:"+user.getAddr());
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
WEB-INF下web.xml
-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>
com.test.controller.UserServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
WebRoot下useredit.jsp代码如下:
-------------------------------------------------------------------------
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.test.model.*" %>
<%@ page import="com.test.util.*" %>
<%@ page import="com.test.service.impl.*"%>
<%@ page import="com.test.service.itf.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>公告管理</title>
<style>
* { margin:0; padding:0; font-size:12px;}
body{ background-color:#fff;}
#wrapper{ margin-left:auto; margin-right:auto; margin:30px;}
#wrapper h3{ font-size:18px;}
a{ text-decoration:none; color:#000;}
a:hover{ text-decoration:underline;}
table{border-collapse:collapse; cellpadding:10px;}
td,th { border: 1px solid #000; padding: 3px 5px 2px 5px;}
#pagination a{display:black;line-height:22px; text-align:center; width:45px; height:22px;border:1px #000 solid; background-color:#FFFFFF;}
#quit{display:black;line-height:22px; text-align:center; width:55px; height:22px;border:1px #000 solid; background-color:#FFFFFF;}
/*-------------impromptu---------- */
.jqifade{ position: absolute; background-color: #aaaaaa; }
div.jqi{ width: 400px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; position: absolute; background-color: #ffffff; font-size: 11px; text-align: left; border: solid 1px #eeeeee; -moz-border-radius: 10px; -webkit-border-radius: 10px; padding: 7px; }
div.jqi .jqicontainer{ font-weight: bold; }
div.jqi .jqiclose{ position: absolute; top: 4px; right: -2px; width: 18px; cursor: default; color: #bbbbbb; font-weight: bold; }
div.jqi .jqimessage{ padding: 10px; line-height: 20px; color: #444444; }
div.jqi .jqibuttons{ text-align: right; padding: 5px 0 5px 0; border: solid 1px #eeeeee; background-color: #f4f4f4; }
div.jqi button{ padding: 3px 10px; margin: 0 10px; background-color: #2F6073; border: solid 1px #f4f4f4; color: #ffffff; font-weight: bold; font-size: 12px; }
div.jqi button:hover{ background-color: #728A8C; }
div.jqi button.jqidefaultbutton{ background-color: #BF5E26; }
.jqiwarning .jqi .jqibuttons{ background-color: #BF5E26; }
/*-------------------------------- */
</style>
<script language="javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-impromptu.2.7.min.js"></script>
<script type="text/javascript">
//增加记录
function addNews(){
var txt = '用户名:<input id="username" name="username" /><br>地址:<input id="addr" name="addr" />';
function mysubmitfunc(v,m,f){
an = m.children('#content');
if(f.content == ""){
an.css("border","solid #ff0000 1px");
return false;
}
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=add",
data:"username="+f.username+"&addr="+f.addr,
success:function(msg){
$("#tips").css("color", "red");
$("#tips").html(msg);
window.location.reload();
}
});
return true;
}
//在对话框添加公告
$.prompt(txt,{
submit: mysubmitfunc,
buttons:{'确定':true}
});
}
//修改记录
function updateNews(){
var newsids = $("input:checked");
if(newsids.length == 0){
$("#tips").css("color", "red");
$("#tips").html("请选择你要进行操作的记录,对其进行操作!");
}
for(var i=0;i<newsids.length;i++){
if(newsids[i].checked){
var old = "";
//通过ajax获取原来的记录内容
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=query",
data:"id="+newsids[i].value,
async :false,
success:function(msg){
old = msg;
}
});
var txt = old+'<br><input type="hidden" id="id" name="id" value='+newsids[i].value+'>用户名:<input id="username" name="username" />';
function mysubmitfunc(v,m,f){
an = m.children('#username');
if(f.content == ""){
an.css("border","solid #ff0000 1px");
return false;
}
//ajax进行异步修改
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=update",
data:"id="+f.id+"&username="+f.username,
success:function(msg){
$("#tips").css("color", "red");
$("#tips").html(msg);
window.location.reload();
}
});
return true;
}
//在对话框修改公告
$.prompt(txt,{
submit: mysubmitfunc,
buttons:{'确定':true}
});
}
}
}
//删除记录
function deleteNews(){
var newsids = $("input:checked");
if(newsids.length == 0){
$("#tips").css("color", "red");
$("#tips").html("请选择你要进行操作的记录,对其进行操作!");
return;
}
for(var i=0;i<newsids.length;i++){
if(newsids[i].checked){
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=del",
data:"id="+newsids[i].value,
success:function(msg){
$("#tips").css("color", "red");
$("#tips").html(msg);
window.location.reload();
}
});
}
}
}
//查询
$(function(){
$("td > a").click(function(){
var id = $(this).attr("name");
var old = "";
//通过ajax获取原来的记录内容
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=query",
data:"id="+id,
async :false,
success:function(msg){
old = msg;
}
});
var txt = old;
//在对话框添加公告
$.prompt(txt,{
buttons:{'确定':true}
});
});
});
</script>
</head>
<body>
<div id="wrapper">
<h3>系统用户管理</h3>
<form action="" name="myform" method="post">
<table>
<tr>
<td>编号</td>
<td>用户</td>
<td>地址</td>
</tr>
<%String str_curPage = request.getParameter("curPage");
PaginationNotice noticep = new PaginationNotice();
if(str_curPage!=null){
noticep.setCurPage(Integer.parseInt(str_curPage));
}
List<User> list = noticep.queryAll();
int allRows = noticep.getAllRows();
int allPages = noticep.getAllPages();
int curPage = noticep.getCurPage();
int rowPerPage = noticep.getRowPerPage();
for(int i=0;i<list.size();i++){
User user = list.get(i);
%>
<tr>
<td><input type="checkbox" name="newsid" class="newsid" value="<%=user.getId()%>" /><%=(curPage-1)*rowPerPage+i+1%></td>
<td><a href="#" name="<%=user.getId()%>"><%=user.getUsername()%></a></td>
<td><%=user.getAddr()%></td>
</tr>
<%}%>
</table>
<br>
<div id="pagination">
<a href="noticelist.jsp?curPage=1">首页</a>
<% if(curPage!=1){ %>
<a href="noticelist.jsp?curPage=<%=curPage-1 %>">上一页</a>
<%}if(allPages>=3){%>
<a href="noticelist.jsp?curPage=2">2</a>
<a href="noticelist.jsp?curPage=3">3</a>
<%}if(curPage!=allPages){ %>
<a href="noticelist.jsp?curPage=<%=curPage+1 %>">下一页</a>
<%} %>
<a href="noticelist.jsp?curPage=<%=allPages %>">末页</a>
</div>
<br>
<input type="button" name="operate" value="增加" onclick="addNews();"/>
<input type="button" name="operate" value="修改" onclick="updateNews();"/>
<input type="button" name="operate" value="删除" onclick="deleteNews();"/>
<br><br>
<p id="tips"></p>
</form>
</div>
</body>
</html>
效果如下:
[/align]
自己写的一个CURD使用了jquery插件jquery-impromptu.2.7.min.js,
一个页面实现一张表的增删改查,无任何跳转,访问很快
项目结构如下:
test数据库,user表
sql:
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(12) default NULL,
`addr` varchar(12) default NULL,
PRIMARY KEY (`id`)
)
util包代码:
-------------------------------------------------------------------------
UtilTool .java
package com.test.util;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
/**
*
* @author zhaiyu
* @see 常用方法工具包
*/
public class UtilTool {
/*
* 验证字符串是否为空
*/
public static boolean isNotBlank(String temp) {
if (temp != null && !"".equals(temp)) {
return true;
} else {
return false;
}
}
/*
* 验证数字是否为空
*/
public static boolean isNotBlank(int temp) {
if (!"".equals(temp) && temp > 0) {
return true;
} else {
return false;
}
}
/*
* 验证数字是否有效
*/
public static boolean isNumAvaliable(int temp) {
if (!"".equals(temp) && temp > 0) {
return true;
} else {
return false;
}
}
}
DbConnection.java
package com.test.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbConnection {
public static Connection getConnection(){
Connection con=null;
String CLASSFORNAME="com.mysql.jdbc.Driver";
String SERVANDDB="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8";
String USER="root";
String PWD="admin";
try{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB,USER,PWD);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
public static void main(String args []){
System.out.println(DbConnection.getConnection());
}
}
PaginationNotice.java
package com.test.util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.test.model.User;
public class PaginationNotice {
private int allRows;
private int curPage = 1;
private int rowPerPage = 10;
private int allPages;
public int getRowPerPage() {
return rowPerPage;
}
public int getAllRows() {
return allRows;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getCurPage() {
return curPage;
}
public int getAllPages() {
return allPages;
}
public List<User> queryAll() {
List<User> list = new ArrayList<User>();
Statement stmt = null;
ResultSet rst = null;
try {
stmt = DbConnection.getConnection()
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rst = stmt.executeQuery("select * from user");
rst.last();
this.allRows = rst.getRow();
if (this.allRows % this.rowPerPage == 0) {
this.allPages = this.allRows / this.rowPerPage;
} else {
this.allPages = this.allRows / this.rowPerPage + 1;
}
if (this.curPage == 1) {
rst.beforeFirst();
} else {
rst.absolute((this.curPage - 1) * this.rowPerPage);
}
int i = 0;
while (rst.next() && i < this.rowPerPage) {
User user = new User();
user.setId(rst.getInt(1));
user.setUsername(rst.getString(2));
user.setAddr(rst.getString(3));
list.add(user);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
model包代码:
-------------------------------------------------------------------------
User.java
package com.test.model;
public class User {
private int id;
private String username;
private String addr;
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 getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
DAO包代码:
-------------------------------------------------------------------------
UserDAO.java
package com.test.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.test.model.User;
import com.test.util.DbConnection;
public class UserDAO {
private Connection con;
/** 通过id查询 */
public User queryById(String id) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
User user = null;
ResultSet rst = null;
try {
pstmt = con.prepareStatement("select * from user where id = ?");
pstmt.setString(1, id);
rst = pstmt.executeQuery();
while (rst.next()) {
user = new User();
user.setId(rst.getInt(1));
user.setUsername(rst.getString(2));
user.setAddr(rst.getString(3));
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
con.close();
}
return user;
}
/** 增加一条记录 */
public void addRecord(User user) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("insert into user (username,addr) values(?,?)");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getAddr());
pstmt.execute();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
con.close();
}
}
/** 删除一条记录 */
public void deleteRecord(int id) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("delete from user where id = ?");
pstmt.setInt(1, id);
pstmt.execute();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
con.close();
}
}
/** 更新一条记录 */
public void update(User user) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("update user set username=?,addr=? where id=?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getAddr());
pstmt.setInt(3, user.getId());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
con.close();
}
}
}
service->itf包代码:
-------------------------------------------------------------------------
package com.test.service.itf;
import com.test.model.User;
/**
* @author zhaiyu
* @see 用户公告表的业务逻辑接口
* @version 2009.8.25
*/
public interface IUserService {
/** 查询一条记录 */
public User getoneRecord(String id) throws Exception;
/** 增加一条记录 */
public void addRecord(User user) throws Exception;
/** 删除一条记录 */
public void deleteUserById(int id) throws Exception;
/** 更新一条记录 */
public void update(User user) throws Exception;
}
service->impl包代码:
-------------------------------------------------------------------------
package com.test.service.impl;
import com.test.dao.UserDAO;
import com.test.model.User;
import com.test.service.itf.IUserService;
public class UserServiceImpl implements IUserService {
private UserDAO dao = new UserDAO();
/** 增加一条记录 */
public void addRecord(User user) throws Exception {
dao.addRecord(user);
}
/** 查询一条记录 */
public User getoneRecord(String id) throws Exception {
return dao.queryById(id);
}
/** 删除一条记录 */
public void deleteUserById(int id) throws Exception {
dao.deleteRecord(id);
}
/** 更新一条记录 */
public void update(User user) throws Exception {
dao.update(user);
}
}
controller包代码:
-------------------------------------------------------------------------
UserServlet.java
package com.test.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.model.User;
import com.test.service.impl.UserServiceImpl;
import com.test.service.itf.IUserService;
import com.test.util.UtilTool;
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = -2446220534127375106L;
private IUserService userv = new UserServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("del".equals(action)) {
String id = request.getParameter("id");
if (UtilTool.isNotBlank(id)) {
try {
userv.deleteUserById(Integer.parseInt(id));
PrintWriter out = response.getWriter();
out.println("删除成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if ("add".equals(action)) {
String username = request.getParameter("username");
String addr = request.getParameter("addr");
if (UtilTool.isNotBlank(username)&&UtilTool.isNotBlank(addr)) {
try {
User user = new User();
user.setUsername(username);
user.setAddr(addr);
userv.addRecord(user);
PrintWriter out = response.getWriter();
out.println("添加成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if ("update".equals(action)) {
String id = request.getParameter("id");
String username = request.getParameter("username");
if (UtilTool.isNotBlank(username) && UtilTool.isNotBlank(id)) {
try {
User user = new User();
int uid = Integer.parseInt(id);
user.setId(uid);
user.setUsername(username);
userv.update(user);
PrintWriter out = response.getWriter();
out.println("公告修改成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if ("query".equals(action)) {
String id = request.getParameter("id");
if (UtilTool.isNotBlank(id)) {
try {
User user = userv.getoneRecord(id);
if (user != null) {
PrintWriter out = response.getWriter();
out
.println("username:"
+ user.getUsername()
+ "<br>"
+ "address:"+user.getAddr());
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
WEB-INF下web.xml
-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>
com.test.controller.UserServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
WebRoot下useredit.jsp代码如下:
-------------------------------------------------------------------------
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.test.model.*" %>
<%@ page import="com.test.util.*" %>
<%@ page import="com.test.service.impl.*"%>
<%@ page import="com.test.service.itf.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>公告管理</title>
<style>
* { margin:0; padding:0; font-size:12px;}
body{ background-color:#fff;}
#wrapper{ margin-left:auto; margin-right:auto; margin:30px;}
#wrapper h3{ font-size:18px;}
a{ text-decoration:none; color:#000;}
a:hover{ text-decoration:underline;}
table{border-collapse:collapse; cellpadding:10px;}
td,th { border: 1px solid #000; padding: 3px 5px 2px 5px;}
#pagination a{display:black;line-height:22px; text-align:center; width:45px; height:22px;border:1px #000 solid; background-color:#FFFFFF;}
#quit{display:black;line-height:22px; text-align:center; width:55px; height:22px;border:1px #000 solid; background-color:#FFFFFF;}
/*-------------impromptu---------- */
.jqifade{ position: absolute; background-color: #aaaaaa; }
div.jqi{ width: 400px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; position: absolute; background-color: #ffffff; font-size: 11px; text-align: left; border: solid 1px #eeeeee; -moz-border-radius: 10px; -webkit-border-radius: 10px; padding: 7px; }
div.jqi .jqicontainer{ font-weight: bold; }
div.jqi .jqiclose{ position: absolute; top: 4px; right: -2px; width: 18px; cursor: default; color: #bbbbbb; font-weight: bold; }
div.jqi .jqimessage{ padding: 10px; line-height: 20px; color: #444444; }
div.jqi .jqibuttons{ text-align: right; padding: 5px 0 5px 0; border: solid 1px #eeeeee; background-color: #f4f4f4; }
div.jqi button{ padding: 3px 10px; margin: 0 10px; background-color: #2F6073; border: solid 1px #f4f4f4; color: #ffffff; font-weight: bold; font-size: 12px; }
div.jqi button:hover{ background-color: #728A8C; }
div.jqi button.jqidefaultbutton{ background-color: #BF5E26; }
.jqiwarning .jqi .jqibuttons{ background-color: #BF5E26; }
/*-------------------------------- */
</style>
<script language="javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-impromptu.2.7.min.js"></script>
<script type="text/javascript">
//增加记录
function addNews(){
var txt = '用户名:<input id="username" name="username" /><br>地址:<input id="addr" name="addr" />';
function mysubmitfunc(v,m,f){
an = m.children('#content');
if(f.content == ""){
an.css("border","solid #ff0000 1px");
return false;
}
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=add",
data:"username="+f.username+"&addr="+f.addr,
success:function(msg){
$("#tips").css("color", "red");
$("#tips").html(msg);
window.location.reload();
}
});
return true;
}
//在对话框添加公告
$.prompt(txt,{
submit: mysubmitfunc,
buttons:{'确定':true}
});
}
//修改记录
function updateNews(){
var newsids = $("input:checked");
if(newsids.length == 0){
$("#tips").css("color", "red");
$("#tips").html("请选择你要进行操作的记录,对其进行操作!");
}
for(var i=0;i<newsids.length;i++){
if(newsids[i].checked){
var old = "";
//通过ajax获取原来的记录内容
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=query",
data:"id="+newsids[i].value,
async :false,
success:function(msg){
old = msg;
}
});
var txt = old+'<br><input type="hidden" id="id" name="id" value='+newsids[i].value+'>用户名:<input id="username" name="username" />';
function mysubmitfunc(v,m,f){
an = m.children('#username');
if(f.content == ""){
an.css("border","solid #ff0000 1px");
return false;
}
//ajax进行异步修改
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=update",
data:"id="+f.id+"&username="+f.username,
success:function(msg){
$("#tips").css("color", "red");
$("#tips").html(msg);
window.location.reload();
}
});
return true;
}
//在对话框修改公告
$.prompt(txt,{
submit: mysubmitfunc,
buttons:{'确定':true}
});
}
}
}
//删除记录
function deleteNews(){
var newsids = $("input:checked");
if(newsids.length == 0){
$("#tips").css("color", "red");
$("#tips").html("请选择你要进行操作的记录,对其进行操作!");
return;
}
for(var i=0;i<newsids.length;i++){
if(newsids[i].checked){
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=del",
data:"id="+newsids[i].value,
success:function(msg){
$("#tips").css("color", "red");
$("#tips").html(msg);
window.location.reload();
}
});
}
}
}
//查询
$(function(){
$("td > a").click(function(){
var id = $(this).attr("name");
var old = "";
//通过ajax获取原来的记录内容
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/webs/UserServlet.do?action=query",
data:"id="+id,
async :false,
success:function(msg){
old = msg;
}
});
var txt = old;
//在对话框添加公告
$.prompt(txt,{
buttons:{'确定':true}
});
});
});
</script>
</head>
<body>
<div id="wrapper">
<h3>系统用户管理</h3>
<form action="" name="myform" method="post">
<table>
<tr>
<td>编号</td>
<td>用户</td>
<td>地址</td>
</tr>
<%String str_curPage = request.getParameter("curPage");
PaginationNotice noticep = new PaginationNotice();
if(str_curPage!=null){
noticep.setCurPage(Integer.parseInt(str_curPage));
}
List<User> list = noticep.queryAll();
int allRows = noticep.getAllRows();
int allPages = noticep.getAllPages();
int curPage = noticep.getCurPage();
int rowPerPage = noticep.getRowPerPage();
for(int i=0;i<list.size();i++){
User user = list.get(i);
%>
<tr>
<td><input type="checkbox" name="newsid" class="newsid" value="<%=user.getId()%>" /><%=(curPage-1)*rowPerPage+i+1%></td>
<td><a href="#" name="<%=user.getId()%>"><%=user.getUsername()%></a></td>
<td><%=user.getAddr()%></td>
</tr>
<%}%>
</table>
<br>
<div id="pagination">
<a href="noticelist.jsp?curPage=1">首页</a>
<% if(curPage!=1){ %>
<a href="noticelist.jsp?curPage=<%=curPage-1 %>">上一页</a>
<%}if(allPages>=3){%>
<a href="noticelist.jsp?curPage=2">2</a>
<a href="noticelist.jsp?curPage=3">3</a>
<%}if(curPage!=allPages){ %>
<a href="noticelist.jsp?curPage=<%=curPage+1 %>">下一页</a>
<%} %>
<a href="noticelist.jsp?curPage=<%=allPages %>">末页</a>
</div>
<br>
<input type="button" name="operate" value="增加" onclick="addNews();"/>
<input type="button" name="operate" value="修改" onclick="updateNews();"/>
<input type="button" name="operate" value="删除" onclick="deleteNews();"/>
<br><br>
<p id="tips"></p>
</form>
</div>
</body>
</html>
效果如下:
相关推荐
本文将通过一个具体的示例来介绍如何使用 jQuery 来实现页面之间的数据传递。 #### 二、技术栈与环境 本示例涉及的主要技术包括: - **jQuery**:用于简化 JavaScript 的 HTML 文档遍历、事件处理、动画以及 Ajax ...
例如,使用jQuery可以轻松实现表单验证、数据异步加载,以及页面元素的动态显示和隐藏,增强了系统的交互性和实时性。 在开发环境中,Eclipse是一款广泛使用的Java集成开发环境(IDE)。它提供了代码编辑、调试、...
在本项目中,"javaEE网页版简易计算器(源码)"是一个基于JavaEE平台的Web应用程序,旨在实现一个简单的在线计算器,支持2位数之间的基本数学运算,包括加法、减法、乘法和除法。这个项目利用了MyEclipse作为开发...
JavaEE中文语言包是专为Java企业版(Java Enterprise Edition,简称JavaEE)开发者设计的,旨在帮助那些希望在Eclipse集成开发环境中使用中文界面的用户。Eclipse是一款广泛使用的开源IDE,支持多种编程语言,其中...
JavaEE是企业级应用开发的重要框架,主要用于构建服务器端应用程序,尤其在Web应用开发中广泛应用。本教程将带你快速入门JavaEE,通过实现一个简单的注册登录功能,掌握基础的JavaEE开发技能。 首先,我们需要了解...
在Web开发中,使用jQuery可以提高开发效率,实现丰富的用户交互。例如,轮播图、下拉菜单、弹窗等常见组件都可能用到jQuery。 5. **Hibernate**:Hibernate是一个优秀的对象关系映射(ORM)框架,简化了Java应用与...
使用javaee实现的网上书店(包含数据库),基本增删改查操作。
JAVAEE中有关JDBC使用的说明书
通过阅读这些手册,开发者可以深入理解JavaEE开发环境的搭建、Spring框架的使用方法、如何配置和管理Apache服务器、怎样编写和部署JSP页面,以及JavaScript在前端开发中的应用。这些知识对于成为一名合格的JavaEE...
在 JSP/JavaEE 中实现在线编辑功能的关键是使用 JavaScript 脚本的强大功能对页面 DOM 对象的内容进行基于 HTML 的自动设置。 实现在线编辑功能需要解决两个关键问题:一是实现编辑区域与自动产生 HTML 代码,二是...
2. **JSP(JavaServer Pages)**:JSP是JavaEE提供的另一种动态网页技术,它允许在HTML页面中嵌入Java代码,方便地实现视图层的开发。JSP的指令、脚本元素、EL(Expression Language)和JSTL(JavaServer Pages ...
在IT行业中,JavaEE是一种广泛应用于企业级应用开发的强大框架,尤其在构建复杂、安全的在线支付系统方面表现卓越。本项目"JavaEE实现的在线支付模块"旨在提供一个高效、安全且可复用的支付解决方案。下面我们将深入...
这个"JavaEE_6_中文API帮助文档"是一个非常宝贵的资源,它提供了JavaEE 6中各种组件和技术的详细接口定义和使用指南。 API(Application Programming Interface)是软件开发者用来与系统或库进行交互的一系列预定义...
同时,文档中的英文描述可以帮助你提高专业英语水平,理解国际上广泛使用的术语和编程习惯。 在学习过程中,应注重理论与实践相结合,尝试搭建JavaEE环境,按照文档中的示例编写代码,以加深对每个组件的理解。此外...
【JavaEE实现网上商城】 JavaEE(Java Platform, Enterprise Edition)是用于开发企业级Web应用程序的框架,它提供了丰富的API和服务来构建分布式、多层的系统,如网上商城。在这个项目中,我们采用的是传统的...
在JavaEE中,模型层通常由JavaBeans或Entity Beans来实现,用于封装和管理数据库中的图书信息。 2. **视图(View)**:这部分主要负责展示数据,通常使用JSP(JavaServer Pages)或者JSF(JavaServer Faces)来创建...
- **JSTL/EL**:在JSP中使用表达式语言(EL)和标准标签库(JSTL)简化页面逻辑。 在项目压缩包文件`mvc`中,可能包含了如下内容: - `src`目录:存放源代码,包括控制器类、模型类、Action类等。 - `WEB-INF`目录...
系统使用JavaEE技术来实现,基于B/S架构的软件系统。 系统的实现过程包括需求分析、系统设计、模块开发和测试等阶段。需求分析阶段,需要确定系统的功能需求和性能要求;系统设计阶段,需要确定系统的架构和模块...
AJAX(Asynchronous JavaScript and XML)技术可以实现页面无刷新更新,提高交互性。 8. **性能优化** 通过缓存策略(如Ehcache)、负载均衡和集群技术,可以提高新闻发布系统的响应速度和并发处理能力。 9. **...
在本项目中,jQueryEasyUI用于实现用户友好的交互体验和美观的页面布局。 3. **EclipseLink**:EclipseLink是Java持久层框架,它是JPA规范的主要实现之一。EclipseLink提供了对象/关系映射(ORM)功能,允许开发者...