基于Struts2 的日志管理系统的Java实现
1、首先,项目的架构如下:
2、com.sxl.dba 中:OracleConnector.java
- package com.sxl.dba;
- import java.sql.*;
- //设计模式1:单例模式
- public class OracleConnector {
- private static Connection conn;
- public static Connection getOracleConn()
- {
- //java bean
- try
- {
- //判断是否有打开过连接,如果打开过则返回已有连接,
- //如果没有打开过则新建立连接
- if(conn==null)
- {
- Class.forName("oracle.jdbc.OracleDriver");
- String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
- String user="system";
- String pass="orcl1234";
- //通过驱动管理器获取 oracle数据库连接
- conn= DriverManager.getConnection(url, user, pass);
- System.out.println("数据库连接成功!");
- }
- }
- catch(Exception ex)
- {
- System.out.println("数据库连接失败:"+ex.getMessage());
- }
- return conn;
- }
- }
3、com.sxl.pojos 中:Log.java 和User.java
- package com.sxl.pojos;
- import java.util.Date;
- public class Log { //对应数据库表名:log
- private int lid; //日志ID
- private String title; //日志标题
- private String content; //日志内容
- private int uid; //用户ID,对应的数据库字段:userid
- private Date date; //日志日期,对应数据库字段:logdate
- public int getLid() {
- return lid;
- }
- public void setLid(int lid) {
- this.lid = lid;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public int getUid() {
- return uid;
- }
- public void setUid(int uid) {
- this.uid = uid;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- //构造函数
- public Log(int lid, String title, String content, int uid, Date date) {
- super();
- this.lid = lid;
- this.title = title;
- this.content = content;
- this.uid = uid;
- this.date = date;
- }
- public Log() {
- super();
- // TODO Auto-generated constructor stub
- }
- @Override
- public String toString() {
- return "Log [lid=" + lid + ", title=" + title + ", content=" + content
- + ", uid=" + uid + ", date=" + date + "]";
- }
- }
- package com.sxl.pojos;
- public class User { //对应数据库表名为:loguser
- private int uid; //用户ID,对应的数据库字段:userid
- private String name; //用户姓名
- private String pass; //用户密码
- public int getUid() {
- return uid;
- }
- public void setUid(int uid) {
- this.uid = uid;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPass() {
- return pass;
- }
- public void setPass(String pass) {
- this.pass = pass;
- }
- public User(int uid, String name, String pass) {
- super();
- this.uid = uid;
- this.name = name;
- this.pass = pass;
- }
- public User() {
- super();
- // TODO Auto-generated constructor stub
- }
- @Override
- public String toString() {
- return "User [uid=" + uid + ", name=" + name + ", pass=" + pass + "]";
- }
- }
4、com.sxl.services 中:LogService.java 和UserService.java
- package com.sxl.services;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import com.sxl.pojos.Log;
- public class LogService {
- private Connection conn=null;
- public LogService() {
- conn=com.sxl.dba.OracleConnector.getOracleConn();
- }
- //根据日志ID查找
- public Log findByLid(int lid) {
- if (conn!=null) {
- try {
- Statement st=conn.createStatement();
- String sql="select * from log where lid='"+lid+"'";
- ResultSet rs=st.executeQuery(sql);
- if (rs.next()) {
- Log log=new Log();
- log.setContent(rs.getString("content"));
- log.setTitle(rs.getString("title"));
- log.setLid(rs.getInt("lid"));
- log.setUid(rs.getInt("uid"));
- return log;
- }else {
- System.out.println("连接出错,未查到!");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- //添加日志
- public boolean add(Log log) {
- try {
- Statement st=conn.createStatement();
- DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
- String date=format1.format(log.getDate());
- String sql="insert into log(title,content,userid,logdate) values('"+log.getTitle()+"','"+log.getContent()+"','"+log.getUid()+"','"+date+"')";
- st.execute(sql);
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- //修改日志
- public boolean modify(Log log) {
- try {
- Statement st=conn.createStatement();
- Date nowDate=new Date();
- DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
- String date=format1.format(nowDate);
- String sql="update log set title='"+log.getTitle()+"',content='"+log.getContent()+"',logdate='"+date+"',where lid="+log.getLid();
- st.execute(sql);
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- //根据用户ID查找
- public List<Log> findByUid(int uid) {
- List<Log> list=new ArrayList<Log>();
- if (conn!=null) {
- try {
- Statement st=conn.createStatement();
- System.out.println("进入查询...");
- String sql="select * from log where userid='"+uid+"'";
- ResultSet rs=st.executeQuery(sql);
- while (rs.next()) {
- System.out.println("查询结果为:");
- Log log=new Log();
- log.setContent(rs.getString("content"));
- log.setTitle(rs.getString("title"));
- log.setLid(rs.getInt("lid"));
- log.setUid(rs.getInt("uid"));
- log.setDate(rs.getDate("date"));
- list.add(log);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return list;
- }
- //删除日志
- public boolean del(int lid) {
- try {
- Statement st=conn.createStatement();
- String sql="delete from log where lid="+lid;
- st.execute(sql);
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- }
- package com.sxl.services;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.sxl.pojos.User;
- public class UserService {
- private Connection conn=null;
- public UserService() {
- conn=com.sxl.dba.OracleConnector.getOracleConn();
- }
- //检查(查找)用户
- public int find(User user) {
- if (conn!=null) {
- try {
- Statement st=conn.createStatement();
- String sql="select userid from loguser where name='"+user.getName().replace('\'', ' ')+"' and pass='"+user.getPass().replace('\'', ' ')+"'";
- ResultSet rs=st.executeQuery(sql);
- if (rs.next()) {
- return rs.getInt("uid");
- }else {
- System.out.println("对不起,该用户不存在");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return -1;
- }
- //添加用户
- public boolean add(User user) {
- try {
- Statement st=conn.createStatement();
- String sql="insert into loguser values('"+user.getUid()+"','"+user.getName()+"','"+user.getPass()+"')";
- st.execute(sql);
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- //修改用户
- public boolean modify(User user) {
- try {
- Statement st=conn.createStatement();
- String sql="update loguser set name='"+user.getName()+"',pass='"+user.getPass()+"' where userid="+user.getUid();
- st.execute(sql);
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- //删除用户
- public boolean del(int uid) {
- try {
- Statement st=conn.createStatement();
- String sql="delete from loguser where userid="+uid;
- st.execute(sql);
- return true;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- }
5、com.sxl.actions 中:LogAction.java 和UserAction.java
- package com.sxl.actions;
- import java.text.DateFormat;
- import java.util.Date;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import java.util.List;
- import com.opensymphony.xwork2.ActionSupport;
- import com.sxl.pojos.Log;
- import com.sxl.services.LogService;
- public class LogAction extends ActionSupport {
- private int uid;
- private int lid;
- private Log log;
- //提取出公共的部分
- private LogService logService=new LogService();
- HttpServletRequest request=ServletActionContext.getRequest();
- public int getUid() {
- return uid;
- }
- public void setUid(int uid) {
- this.uid = uid;
- }
- public int getLid() {
- return lid;
- }
- public void setLid(int lid) {
- this.lid = lid;
- }
- public Log getLog() {
- return log;
- }
- public void setLog(Log log) {
- this.log = log;
- }
- public String add() throws Exception {
- log.setDate(new Date());
- if (logService.add(log)) {
- return "success";
- }else {
- return "failed";
- }
- }
- public String getAllLog() throws Exception{
- List<Log> logList=logService.findByUid(uid);
- request.setAttribute("logList", logList);
- return "getAllLog";
- }
- public String getByLid() throws Exception{
- Log log=logService.findByLid(lid);
- request.setAttribute("log", log);
- return "logModify";
- }
- public String del() throws Exception{
- if (logService.del(lid)) {
- List<Log> logList=logService.findByUid(uid);
- request.setAttribute("logList", logList);
- return "success";
- }
- return "failed";
- }
- public String modify () throws Exception {
- System.out.println("修改:"+log.toString());
- if (logService.modify(log)) {
- return "success";
- }
- return "failed";
- }
- }
- package com.sxl.actions;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.sxl.pojos.User;
- import com.sxl.services.LogService;
- import com.sxl.services.UserService;
- public class UserAction extends ActionSupport {
- private User user;
- private String validatePass;
- //提取出公共的部分
- private UserService userService=new UserService();
- HttpServletRequest request=ServletActionContext.getRequest();
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public String getValidatePass() {
- return validatePass;
- }
- public void setValidatePass(String validatePass) {
- this.validatePass = validatePass;
- }
- //用户登录
- public String login() throws Exception {
- int uid=userService.find(user);
- if (uid!=-1) {
- user.setUid(uid);
- request.getSession().setAttribute("user", user);
- return "personalCenter";
- }else {
- return "failed";
- }
- }
- //添加用户
- public String add() throws Exception {
- if (!user.getPass().equals(validatePass)) {
- return "failed";
- }
- if (userService.add(user)) {
- return "personalCenter";
- }else {
- return "failed";
- }
- }
- public String modify() throws Exception{
- if (!user.getPass().equals(validatePass)) {
- return "failed";
- }if (userService.modify(user)) {
- return "success";
- }else {
- return "failed";
- }
- }
- }
6、struts.xml 配置文件如下:
对应的代码:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="logsystem" namespace="/" extends="struts-default">
- <action name="user" class="com.sxl.actions.UserAction">
- <result name="personalCenter" type="dispatcher">/personalCenter.jsp</result>
- <result name="success" type="dispatcher">/success.jsp</result>
- <result name="failed" type="redirect">/failed.jsp</result>
- </action>
- <action name="log" class="com.sxl.actions.LogAction">
- <result name="getAllLog" type="dispatcher">/getAllLog.jsp</result>
- <result name="success" type="dispatcher">/success.jsp</result>
- <result name="logModify" type="dispatcher">/logModify.jsp</result>
- <result name="failed" type="redirect">/failed.jsp</result>
- </action>
- </package>
- </struts>
& jsp s~
基于Struts2 的日志管理系统的Java实现的更多相关文章
- 基于struts2的学生报道管理系统(附github源码地址)
本项目参考了<java web轻量级开发全体验>,加入了对mysql的支持. 一.基本业务功能 通过struts2框架,结合mysql数据库构建一个学生报到管理系统,来模拟学生报到登记的过 ...
- 基于SSH实现员工管理系统之框架整合篇
本篇文章来源于:https://blog.csdn.net/zhang_ling_yun/article/details/77803178 以下内容来自慕课网的课程:基于SSH实现员工管理系统之框架整 ...
- 离线部署ELK+kafka日志管理系统【转】
转自 离线部署ELK+kafka日志管理系统 - xiaoxiaozhou - 51CTO技术博客http://xiaoxiaozhou.blog.51cto.com/4681537/1854684 ...
- 日志管理系统ELK6.2.3
https://www.jianshu.com/p/88f2cbedcc2a 写在前面 刚毕业工作的时候,处理日志喜欢自己写脚本抓取数据分析日志,然后在zabbix上展示出来.但是开发要看日志的时候, ...
- 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验
一,使用Struts2标签库简化表单 在文章[基于Struts2框架实现登录案例]的基础上,通过使用Struts标签库可以简化登录页面login2.jsp <%@ page language=& ...
- 【课程分享】基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构、自己定义工作流)
基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构.自己定义工作流) 课程讲师:张弘 课程分类:Java 适合人群:中级 课时数量:37课时 用到技术:Spring ...
- Eventlog Analyzer日志管理系统、日志分析工具、日志服务器的功能及作用
Eventlog Analyzer日志管理系统.日志分析工具.日志服务器的功能及作用 Eventlog Analyzer是用来分析和审计系统及事件日志的管理软件,能够对全网范围内的主机.服务器.网络设 ...
- 基于hive的日志分析系统
转自 http://www.cppblog.com/koson/archive/2010/07/19/120773.html hive 简介 hive 是一个基于 ...
- 基于UML的毕业设计管理系统的分析与设计
基于UML的毕业设计管理系统的分析与设计 <本段与标题无关,自行略过 最近各种忙,天气不错,导师心情不错:“我们要写一个关于UML的专著”,一句话:“一个完整的系统贯穿整个UML的知识”:我:“ ...
随机推荐
- codevs 4909 寂寞的堆(写的好丑0.0)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #defin ...
- 一些 Windows 系统不常见的 鼠标光标常数
一些 Windows 系统不常见的 鼠标光标常数 Private Declare Function SetCursor Lib "user32" (ByVal hCursor A ...
- OJ常见问题及必须认识的对拍处理水题
HDUOJ: 常见问题及解答 Q: Online Judge(以下简称OJ)支持哪些语言? A: 目前为止,HDOJ支持C.C++.Pascal和Java四种语言. Q: 有什么条件判断我的程序是在O ...
- [转帖]了解AmbiLight知识
了解科技前沿的方法..American Online=AOL.algorithm算法.(Denzel say, and I don't know) Engadget瘾科技网站.英文版Engadget网 ...
- UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:
2015-01-28 21:55:17.790 Demo[636:9351] the behavior of the UICollectionViewFlowLayout is notdefined ...
- 【OpenSSL】创建证书
[-] 1生成根证书 1 生成RSA私钥 2 生成证书请求 3 签发自签名证书 2 生成用户证书 1 生成RSA私钥 2 生成证书请求 3 签发证书 1)生成根证书 1.1) 生成RSA私钥 op ...
- 关于SVD(Singular Value Decomposition)的那些事儿
SVD简介 SVD不仅是一个数学问题,在机器学习领域,有相当多的应用与奇异值都可以扯上关系,比如做feature reduction的PCA,做数据压缩(以图像压缩为代表)的算法,还有做搜索引擎语义层 ...
- Hibernate 性能优化之二级缓存
二级缓存是一个共享缓存,在二级缓存中存放的数据是共享数据特性 修改不能特别频繁 数据可以公开二级缓存在sessionFactory中,因为sessionFactory本身是线程安全,所 ...
- text-overflow:ellipsis 的应用(转载)
关键字: text-overflow:ellipsis 语法:text-overflow : clip | ellipsis 取值: clip :默认值 .不显示省略标记(...),而是简单的裁切. ...
- Thinkphp 零散知识点(caa/js路径,引入第三方类,ajax返回,session/cookie)
一.关于JS和CSS路径问题 1.找路径是从入口文件index.php来找的,而不是从文件本身所在位置来找, 因为我们访问时是访问的入口文件 2.在存放JS和CSS的时候可以放到public文件夹下 ...