登陆和增删改查的全部代码例子:http://download.youkuaiyun.com/detail/asdfzjs/6269881
1,首先通过用myeclipse,new一个web project,如下图:
2,点击finsh,然后右键点击我的项目project,选择Myeclipse,再选择add struts,如下图:
3,可以直接点击finish。这样配置就可以算结束了。
4,下面是包的搭建(仅供参考);
5,util包里面的操作数据库的:
package com.sims.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @title HelperClass
* @description 系统帮助类
* @author tanqi
* @version
* @create_date May 9, 2013
* @copyright Yancheng Teachers University
*
*/
public class HelperClass {
/**
*
* @title getConn
* @description 连接驱动
* @author zjs
* @create_date May 9, 2013
* @return
*/
public static final Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sims??useUnicode=true&characterEncoding=utf-8&autoReconnect=true", "root", "123456");
} catch (ClassNotFoundException e) {
System.out.println("异常信息为:"+e.getMessage()+"OK");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("异常信息为:"+e.getMessage()+"OK");
e.printStackTrace();
}
return conn;
}
/**
*
* @title close
* @description 关闭链接
* @author tanqi
* @create_date May 9, 2013
* @param conn 连接对象
* @param s 事务处理
* @param rs 结果集
*/
public static final void close(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection!= null) {
connection.close();
}
if (statement!= null) {
statement.close();
}
if (resultSet!= null) {
resultSet.close();
}
} catch (SQLException e) {
System.out.println("异常信息为:"+e.getMessage()+"OK");
e.printStackTrace();
}
}
}
6,action包里的UserForm
package com.sims.userManager.action.from;
import org.apache.struts.action.ActionForm;
/**
*
* @title userInfor
* @description 用户信息持久类
* @author zjs
* @version V1.0
* @create_date May 9, 2013
* @copyright Yancheng Teachers University
*
*/
@SuppressWarnings("serial")
public class UserForm extends ActionForm {
/**
* 用户唯一标识Id
*/
private int userId;
/**
* 用户姓名
*/
private String userName;
/**
* 用户密码
*/
private String userPwd;
/**
* 用户年龄
*/
private int userAge;
/**
* @return the userId
*/
public int getUserId() {
return userId;
}
/**
* @param userId
* the userId to set
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName
* the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the userPwd
*/
public String getUserPwd() {
return userPwd;
}
/**
* @param userPwd
* the userPwd to set
*/
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
/**
* @return the userAge
*/
public int getUserAge() {
return userAge;
}
/**
* @param userAge
* the userAge to set
*/
public void setUserAge(int userAge) {
this.userAge = userAge;
}
}
UserLoginAction
package com.sims.userManager.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.BaseAction;
import com.sims.userManager.action.from.UserForm;
import com.sims.userManager.service.impl.UserLoginIMPL;
/**
*
* @title UserLoginAction
* @description 用户登录action类
* @author zjs
* @version
* @create_date May 11, 2013
* @copyright Yancheng Teachers University
*
*/
public class UserLoginAction extends BaseAction {
/*
* @title execute @author Administrator
*
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
* org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String method = request.getParameter("method");
if ("login".equals(method)) {
return this.loginIn(mapping, form, request, response);
} else {
return mapping.findForward("showError");
}
}
/**
*
* @title loginIn
* @description 用户登录action类
* @author zjs
* @create_date May 11, 2013
* @param mapping 映射
* @param form 表单
* @param request 请求
* @param response 响应
* @return
*/
public ActionForward loginIn(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
String userName = userForm.getUserName();
String userPwd = userForm.getUserPwd();
try {
@SuppressWarnings("unused")
String userString=new String(userName.getBytes("ISO8859-1"),"utf-8");//转换编码
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
UserLoginIMPL userLoginIMPL = new UserLoginIMPL();
int flag = userLoginIMPL.loginIn(userName, userPwd);
if (flag == 1) {
return mapping.findForward("login");
} else {
request.setAttribute("flag", "0");
return mapping.findForward("loginError");
}
}
}
7,service层
UserLoginService:
package com.sims.userManager.service;
/**
*
* @title UserLoginService
* @description 用户登录service接口
* @author zjs
* @version
* @create_date May 11, 2013
* @copyright Yancheng Teachers University
*
*/
public interface UserLoginService {
/**
*
* @title loginIn
* @description 用户登录接口方法
* @author zjs
* @create_date May 11, 2013
* @param userName
* @param usePwd
* @return 成功与否的标识
*/
public int loginIn(String userName, String userPwd);
}
UserLoginIMPL:
package com.sims.userManager.service.impl;
import com.sims.userManager.dao.jdbc.UserLoginJDBC;
import com.sims.userManager.service.UserLoginService;
/**
*
* @title UserLoginIMPL
* @description 用户登录service实现类
* @author zjs
* @version
* @create_date May 11, 2013
* @copyright Yancheng Teachers University
*
*/
public class UserLoginIMPL implements UserLoginService {
/*
*
* @title loginIn @author tanqi
*
* @see com.sims.userManager.service.UserLoginService#loginIn(java.lang.String,
* java.lang.String)
*/
public int loginIn(String userName, String userPwd) {
UserLoginJDBC userLoginJDBC=new UserLoginJDBC();
boolean yesORno=userLoginJDBC.loginIn(userName, userPwd);
if (yesORno==true) {
return 1;
}else {
return 0;
}
}
}
8dao层:
UserLoginDao:
package com.sims.userManager.dao;
/**
*
* @title UserLoginDao
* @description 用户登录dao接口
* @author zjs
* @version
* @create_date May 11, 2013
* @copyright Yancheng Teachers University
*
*/
public interface UserLoginDao {
/**
*
* @title loginIn
* @description 用户登录dao接口方法
* @author tanqi
* @create_date May 11, 2013
* @param userName 用户名
* @param userPwd 用户密码
* @return 是否登录成功
*/
public boolean loginIn(String userName,String userPwd);
}
UserLoginJDBC:
package com.sims.userManager.dao.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.sims.userManager.dao.UserLoginDao;
import com.sims.util.HelperClass;
/**
*
* @title UserLoginJDBC
* @description 用户登录dao实现类
* @author zjs
* @version
* @create_date May 11, 2013
* @copyright Yancheng Teachers University
*
*/
public class UserLoginJDBC implements UserLoginDao {
/*
*
* @title loginIn @author tanqi
*
* @see com.sims.userManager.dao.UserLoginDao#loginIn(java.lang.String,
* java.lang.String)
*/
public boolean loginIn(String userName, String userPwd) {
Connection connection = HelperClass.getConnection();
Statement statement = null;
ResultSet resultSet = null;
int flag = 1;
String sql = "select * from userInfor where userName='"
+ userName + "' and userPwd='" + userPwd + "'";
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
resultSet.next();
flag = resultSet.getRow();
} catch (SQLException e) {
System.out.println("异常信息为:"+e.getMessage()+"OK");
e.printStackTrace();
} finally {
HelperClass.close(connection, statement, resultSet);
}
if (flag == 1) {
return true;
} else {
return false;
}
}
}
前台:
index.jsp
<jsp:forward page="/jsp/loginIn.jsp"/>
下面的jsp页面是单独放在jsp文件夹下面的:
loginIn.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!--jstl标签库t-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<style>
html,body{margin:0;}
image{border:0;}
.bg{background:url(images/quick_login_bg.gif) repeat-x;height:auto;width:100%;}
body{background:url(images/quick_login_bg.gif) repeat-x;}
a{text-decoration:none;color:#999;}
.head{width:960px;height:112px;margin-left:auto;margin-right:auto;background-image:url(images/quick_login_head.jpg);background-size:cover;}
.bgfoot{height:50px;width:960px;margin-left:auto;margin-right:auto;}
.middle{height:350px;width:960px;margin-left:auto;margin-right:auto;}
.login{height:300px;width:307px;margin:auto;margin-top:30px;background:url(images/quick_login_form_pic.gif);background-size:cover;}
</style>
<body>
<script language="javascript">
function checkpassword()
{
var password=document.getElementById("pass").value;
if(password=="")
{
document.getElementById("span_password").innerHTML="请输入密码!";
document.getElementById("pass").style.borderColor="#F00";
return false;
}
document.getElementById("span_password").innerHTML="";
document.getElementById("pass").style.borderColor="";
}
function checkusername()
{
var username=document.getElementById("name").value;
if(username=="")
{
document.getElementById("span_name").innerHTML="请输入用户名!";
document.getElementById("name").style.borderColor="#F00";
return false;
}
document.getElementById("span_name").innerHTML="";
document.getElementById("name").style.borderColor="";
}
</script>
<div class="bg">
<div class="head">
<div style="height:65px;width:200px;float:left;margin-top:20px;">
<a href="#"><img src="images/logo.png" style="height:78px; width:200px;"></a>
</div>
<div style="float:left; padding-top:20px; margin-left:150px;">
<font style="font-family:微软雅黑; font-size:38px;" >库存信息管理系统</font>
</div>
</div>
<div class="middle">
<div class="login">
<div style="height:60px;">
<table>
<tr style="height:5px;"></tr>
<tr>
<td style="width:20px;"></td>
<td>用户登录</td>
</tr>
</table>
</div>
<div style="margin-top:70px;margin:auto;height:150px;width:200px;">
<form action="userLoginAction.do?method=login" method="post">
<c:if test="${flag ==0}">
<div>
<font color="red">用户登录错误!</font>
</div>
</c:if>
账号:<input style="border:solid 1px #CCCCCC;" type="text" name="userName" id="name" onblur="checkusername()"/><span id="span_name" style="color:#993300"></span><br>
<div style="margin-top:20px;"></div>
密码:<input style="border:solid 1px #CCCCCC;" type="password" name="userPwd" id="pass" onblur="checkpassword()"/><span id="span_password" style="color:#993300"></span><br/>
<div style="margin-top:20px;"></div>
<input style="width:100px;height:35px;margin-left:40px;margin-top:20px;font-size:14px;color:#333;" type="submit" value="登录"/>
</form></div>
</div>
</div>
<div class="bgfoot">
<div style="width:450px;height:50px;margin-left:auto;margin-right:auto;">
<table style="font-size:12px;">
<tr style="height:10px;"></tr>
<tr>
<td><a href="#">关于我们|</a></td>
<td><a href="#">法律声明|</a></td>
<td><a href="#">操作流程|</a></td>
<td><a href="#">服务条款|</a></td>
<td><a href="#">联系我们|</a></td>
<td><a href="#">友情链接</a></td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>错误信息反馈页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This action is error! Please check!<br>
</body>
</html>
main.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="com.sims.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="com.sims.productManager.action.form.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@page import="com.sims.productManager.action.form.ProductForm"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!--jstl标签库t-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>库存信息系统首页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script language="javascript" type="text/javascript">
function bodyLoad(){
var dateTime=new Date();
var hh=dateTime.getHours();
var mm=dateTime.getMinutes();
var ss=dateTime.getSeconds();
var yy=dateTime.getFullYear();
var MM=dateTime.getMonth()+1; //因为1月这个方法返回为0,所以加1
var dd=dateTime.getDate();
var week=dateTime.getDay();
var days=[ "日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 "] ;
document.getElementById("date").innerHTML =yy+"年"+MM+"月"+dd+"日 "+"星期"+days[week]+hh+"时"+mm+"分"+ss+"秒" ;
setTimeout(bodyLoad,1000);
}
</script>
<style type="text/css">
html,body {margin:0;padding:0}
body{
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
IFRAME {border:solid 0px #CCC;}
</style>
<script language="javascript">
function checkcount()
{
var count=document.getElementById("count").value;
if(count=="")
{
document.getElementById("span_count").innerHTML="请输入商品数量";
document.getElementById("count").style.borderColor="#F00";
return false;
}
document.getElementById("span_count").innerHTML="";
document.getElementById("count").style.borderColor="";
}
function checkstyle()
{
var style=document.getElementById("style").value;
if(style=="")
{
document.getElementById("span_style").innerHTML="请输入商品类型!";
document.getElementById("style").style.borderColor="#F00";
return false;
}
document.getElementById("span_password").innerHTML="";
document.getElementById("pass").style.borderColor="";
}
function checkusername()
{
var username=document.getElementById("name").value;
if(username=="")
{
document.getElementById("span_name").innerHTML="请输入商品名称!";
document.getElementById("name").style.borderColor="#F00";
return false;
}
document.getElementById("span_name").innerHTML="";
document.getElementById("name").style.borderColor="";
}
</script>
</head>
<body onload="bodyLoad()" style=" background-color:#FBFBFB;">
<div style="margin-left:auto;margin-right:auto;text-align:center;">
<iframe name="head" framespacing="0" width="100%" frameborder="0" height="75" scrolling="no" marginwidth="0" marginheight="0" src="top.html">
</iframe>
<!--<center>
<label id="date" color="red"></label>
时间代码
</center>-->
<div style=" width:200px; margin-left:150px; font-size:18px;font-family:微软雅黑; text-align:left;">
<font style=" color:red; font-size:18px;">产品增加:</font>
<form action="SaveproductAction.do?method=addProduct" method="post">
商品名称: <input type="text" name="productName" id="name" onblur="checkusername("><span id="span_name" style="color:#993300"></span><br/>
商品类型: <input type="text" name="productStyle" id="style" onblur="checkstyle()"><span id="span_style" style="color:#993300"></span><br/>
商品数量: <input type="text" name="productCount" id="count" onblur="checkcount()"><span id="span_count" style="color:#993300"></span><br/>
日志结果: <input type="text" name="logInfor"><br/>
<input type="reset" value="重置">
<input type="submit" value="增加">
</form>
<c:if test="${a==1}">
<tr>
<td colspan="2">
<font color="red">增加成功</font>
</td>
</tr>
</c:if>
</div>
<center>
<a href="productAction.do?method=showProduct">库存信息查询:</a>
<br/>
<table border="1px,100%,100%,red">
<!--<c:forEach items="${productList}" var="product"><!--var迭代参数的名字 items:要进行迭代的集合-->
<!-- <tr>
<td>${product.productId}</td>
<td>${product.productName}</td>
<td>${product.productStyle}</td>
<td>${product.productCount}</td>
<td><a href="DeleteproductAction.do?method=deleteProduct&productId=${product.productId}">删除</a></td>
<td><a href="#" >修改</td>
</tr>
</c:forEach>-->
<%
List<ProductForm> productList = null;
productList = (List<ProductForm>) request.getAttribute("productList");
String pageStr = request.getParameter("page");
int currentPage = 1;
if (pageStr != null)
currentPage = Integer.parseInt(pageStr);
PageUtil pUtil = new PageUtil(10, productList.size(), currentPage);
currentPage = pUtil.getCurrentPage();
%>
<%
ProductForm model = new ProductForm();
for (int i = pUtil.getFromIndex(); i < pUtil.getToIndex(); i++) {
model = (ProductForm) productList.get(i);
}
%>
<tr><td width=100% bgcolor="#eeeeee" colspan=4 align="center">
记录总数<%=pUtil.getRecordCount()%>条 当前页/总页数<%=currentPage%>
/<%=pUtil.getPageCount()%>每页显示<%=pUtil.getPageSize()%>条
<a href="publishers.jsp?page=1">首页</a>
<a href="publishers.jsp?page=<%=(currentPage - 1)%>">上页</a>
<a href="publishers.jsp?page=<%=(currentPage + 1)%>">下页</a>
<a href="publishers.jsp?page=<%=pUtil.getPageCount()%>">末页</a>
</td></tr>
</table>
</center>
<center>
<h1>库存信息删除:</h1>
<form action="DeleteproductAction.do?method=deleteProduct" method="post">
id:<input type="text" name="productId">
<input type="submit" value="删除">
</form>
<c:if test="${b==0}">
<tr>
<td colspan="2">
<font color="red">删除失败</font>
</td>
</tr>
</c:if>
</center>
<center>
<h1>库存信息修改:</h1>
<form action="ModifyproductAction.do?method=modifyProduct" method="post">
需要修改的id:<input type="text" name="productId"><br/>
商品名称: <input type="text" name="productName"><br/>
商品类型: <input type="text" name="productStyle"><br/>
商品数量: <input type="text" name="productCount"><br/>
日志结果: <input type="text" name="logInfor"><br/>
<input type="reset" value="重置">
<input type="submit" value="修改">
</form>
<c:if test="${c==0}">
<tr>
<td colspan="2">
<font color="red">修改失败</font>
</td>
</tr>
</c:if>
</center>
</div>
</body>
</html>
好了,代码都在上面了,其中增删改查部分代码没有发上去,如果有需要的可以留言。全部代码例子:http://download.youkuaiyun.com/detail/asdfzjs/6269881