1、首先,项目的架构如下:

        

2、com.sxl.dba 中:OracleConnector.java

  1. package com.sxl.dba;
  2. import java.sql.*;
  3. //设计模式1:单例模式
  4. public class OracleConnector {
  5. private static Connection conn;
  6. public static Connection getOracleConn()
  7. {
  8. //java bean
  9. try
  10. {
  11. //判断是否有打开过连接,如果打开过则返回已有连接,
  12. //如果没有打开过则新建立连接
  13. if(conn==null)
  14. {
  15. Class.forName("oracle.jdbc.OracleDriver");
  16. String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
  17. String user="system";
  18. String pass="orcl1234";
  19.  
  20. //通过驱动管理器获取 oracle数据库连接
  21. conn= DriverManager.getConnection(url, user, pass);
  22. System.out.println("数据库连接成功!");
  23. }
  24. }
  25. catch(Exception ex)
  26. {
  27. System.out.println("数据库连接失败:"+ex.getMessage());
  28. }
  29.  
  30. return conn;
  31. }
  32. }

3、com.sxl.pojos 中:Log.java 和User.java

  1. package com.sxl.pojos;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Log { //对应数据库表名:log
  6. private int lid; //日志ID
  7. private String title; //日志标题
  8. private String content; //日志内容
  9. private int uid; //用户ID,对应的数据库字段:userid
  10. private Date date; //日志日期,对应数据库字段:logdate
  11.  
  12. public int getLid() {
  13. return lid;
  14. }
  15. public void setLid(int lid) {
  16. this.lid = lid;
  17. }
  18. public String getTitle() {
  19. return title;
  20. }
  21. public void setTitle(String title) {
  22. this.title = title;
  23. }
  24. public String getContent() {
  25. return content;
  26. }
  27. public void setContent(String content) {
  28. this.content = content;
  29. }
  30. public int getUid() {
  31. return uid;
  32. }
  33. public void setUid(int uid) {
  34. this.uid = uid;
  35. }
  36. public Date getDate() {
  37. return date;
  38. }
  39. public void setDate(Date date) {
  40. this.date = date;
  41. }
  42. //构造函数
  43. public Log(int lid, String title, String content, int uid, Date date) {
  44. super();
  45. this.lid = lid;
  46. this.title = title;
  47. this.content = content;
  48. this.uid = uid;
  49. this.date = date;
  50. }
  51. public Log() {
  52. super();
  53. // TODO Auto-generated constructor stub
  54. }
  55. @Override
  56. public String toString() {
  57. return "Log [lid=" + lid + ", title=" + title + ", content=" + content
  58. + ", uid=" + uid + ", date=" + date + "]";
  59. }
  60.  
  61. }
  1. package com.sxl.pojos;
  2.  
  3. public class User { //对应数据库表名为:loguser
  4. private int uid; //用户ID,对应的数据库字段:userid
  5. private String name; //用户姓名
  6. private String pass; //用户密码
  7.  
  8. public int getUid() {
  9. return uid;
  10. }
  11. public void setUid(int uid) {
  12. this.uid = uid;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getPass() {
  21. return pass;
  22. }
  23. public void setPass(String pass) {
  24. this.pass = pass;
  25. }
  26.  
  27. public User(int uid, String name, String pass) {
  28. super();
  29. this.uid = uid;
  30. this.name = name;
  31. this.pass = pass;
  32. }
  33. public User() {
  34. super();
  35. // TODO Auto-generated constructor stub
  36. }
  37. @Override
  38. public String toString() {
  39. return "User [uid=" + uid + ", name=" + name + ", pass=" + pass + "]";
  40. }
  41. }

4、com.sxl.services 中:LogService.java 和UserService.java

  1. package com.sxl.services;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.List;
  12.  
  13. import com.sxl.pojos.Log;
  14.  
  15. public class LogService {
  16. private Connection conn=null;
  17.  
  18. public LogService() {
  19. conn=com.sxl.dba.OracleConnector.getOracleConn();
  20. }
  21.  
  22. //根据日志ID查找
  23. public Log findByLid(int lid) {
  24. if (conn!=null) {
  25. try {
  26. Statement st=conn.createStatement();
  27. String sql="select * from log where lid='"+lid+"'";
  28. ResultSet rs=st.executeQuery(sql);
  29.  
  30. if (rs.next()) {
  31. Log log=new Log();
  32. log.setContent(rs.getString("content"));
  33. log.setTitle(rs.getString("title"));
  34. log.setLid(rs.getInt("lid"));
  35. log.setUid(rs.getInt("uid"));
  36. return log;
  37. }else {
  38. System.out.println("连接出错,未查到!");
  39. }
  40. } catch (SQLException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44.  
  45. }
  46. return null;
  47. }
  48.  
  49. //添加日志
  50. public boolean add(Log log) {
  51. try {
  52. Statement st=conn.createStatement();
  53.  
  54. DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
  55. String date=format1.format(log.getDate());
  56.  
  57. String sql="insert into log(title,content,userid,logdate) values('"+log.getTitle()+"','"+log.getContent()+"','"+log.getUid()+"','"+date+"')";
  58. st.execute(sql);
  59. return true;
  60. } catch (SQLException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64. return false;
  65. }
  66.  
  67. //修改日志
  68. public boolean modify(Log log) {
  69. try {
  70. Statement st=conn.createStatement();
  71. Date nowDate=new Date();
  72. DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
  73. String date=format1.format(nowDate);
  74. String sql="update log set title='"+log.getTitle()+"',content='"+log.getContent()+"',logdate='"+date+"',where lid="+log.getLid();
  75. st.execute(sql);
  76. return true;
  77. } catch (SQLException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }
  81. return false;
  82. }
  83.  
  84. //根据用户ID查找
  85. public List<Log> findByUid(int uid) {
  86. List<Log> list=new ArrayList<Log>();
  87. if (conn!=null) {
  88. try {
  89. Statement st=conn.createStatement();
  90. System.out.println("进入查询...");
  91. String sql="select * from log where userid='"+uid+"'";
  92. ResultSet rs=st.executeQuery(sql);
  93. while (rs.next()) {
  94. System.out.println("查询结果为:");
  95. Log log=new Log();
  96.  
  97. log.setContent(rs.getString("content"));
  98. log.setTitle(rs.getString("title"));
  99. log.setLid(rs.getInt("lid"));
  100. log.setUid(rs.getInt("uid"));
  101. log.setDate(rs.getDate("date"));
  102.  
  103. list.add(log);
  104. }
  105. } catch (SQLException e) {
  106. // TODO Auto-generated catch block
  107. e.printStackTrace();
  108. }
  109. }
  110. return list;
  111. }
  112.  
  113. //删除日志
  114. public boolean del(int lid) {
  115. try {
  116. Statement st=conn.createStatement();
  117. String sql="delete from log where lid="+lid;
  118. st.execute(sql);
  119. return true;
  120. } catch (SQLException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. return false;
  125. }
  126. }
  1. package com.sxl.services;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import com.sxl.pojos.User;
  9.  
  10. public class UserService {
  11. private Connection conn=null;
  12.  
  13. public UserService() {
  14. conn=com.sxl.dba.OracleConnector.getOracleConn();
  15. }
  16.  
  17. //检查(查找)用户
  18. public int find(User user) {
  19. if (conn!=null) {
  20. try {
  21. Statement st=conn.createStatement();
  22. String sql="select userid from loguser where name='"+user.getName().replace('\'', ' ')+"' and pass='"+user.getPass().replace('\'', ' ')+"'";
  23. ResultSet rs=st.executeQuery(sql);
  24.  
  25. if (rs.next()) {
  26. return rs.getInt("uid");
  27. }else {
  28. System.out.println("对不起,该用户不存在");
  29. }
  30. } catch (SQLException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. return -1;
  36. }
  37.  
  38. //添加用户
  39. public boolean add(User user) {
  40. try {
  41. Statement st=conn.createStatement();
  42. String sql="insert into loguser values('"+user.getUid()+"','"+user.getName()+"','"+user.getPass()+"')";
  43. st.execute(sql);
  44. return true;
  45. } catch (SQLException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. return false;
  50. }
  51.  
  52. //修改用户
  53. public boolean modify(User user) {
  54. try {
  55. Statement st=conn.createStatement();
  56. String sql="update loguser set name='"+user.getName()+"',pass='"+user.getPass()+"' where userid="+user.getUid();
  57. st.execute(sql);
  58. return true;
  59. } catch (SQLException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }
  63. return false;
  64. }
  65.  
  66. //删除用户
  67. public boolean del(int uid) {
  68. try {
  69. Statement st=conn.createStatement();
  70. String sql="delete from loguser where userid="+uid;
  71. st.execute(sql);
  72. return true;
  73. } catch (SQLException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. }
  77. return false;
  78. }
  79. }

5、com.sxl.actions 中:LogAction.java 和UserAction.java

  1. package com.sxl.actions;
  2.  
  3. import java.text.DateFormat;
  4. import java.util.Date;
  5.  
  6. import javax.servlet.http.HttpServletRequest;
  7.  
  8. import org.apache.struts2.ServletActionContext;
  9.  
  10. import java.util.List;
  11. import com.opensymphony.xwork2.ActionSupport;
  12.  
  13. import com.sxl.pojos.Log;
  14. import com.sxl.services.LogService;
  15.  
  16. public class LogAction extends ActionSupport {
  17. private int uid;
  18. private int lid;
  19. private Log log;
  20.  
  21. //提取出公共的部分
  22. private LogService logService=new LogService();
  23. HttpServletRequest request=ServletActionContext.getRequest();
  24.  
  25. public int getUid() {
  26. return uid;
  27. }
  28.  
  29. public void setUid(int uid) {
  30. this.uid = uid;
  31. }
  32.  
  33. public int getLid() {
  34. return lid;
  35. }
  36.  
  37. public void setLid(int lid) {
  38. this.lid = lid;
  39. }
  40.  
  41. public Log getLog() {
  42. return log;
  43. }
  44.  
  45. public void setLog(Log log) {
  46. this.log = log;
  47. }
  48.  
  49. public String add() throws Exception {
  50. log.setDate(new Date());
  51. if (logService.add(log)) {
  52. return "success";
  53. }else {
  54. return "failed";
  55. }
  56. }
  57.  
  58. public String getAllLog() throws Exception{
  59. List<Log> logList=logService.findByUid(uid);
  60. request.setAttribute("logList", logList);
  61. return "getAllLog";
  62. }
  63.  
  64. public String getByLid() throws Exception{
  65. Log log=logService.findByLid(lid);
  66. request.setAttribute("log", log);
  67. return "logModify";
  68. }
  69.  
  70. public String del() throws Exception{
  71. if (logService.del(lid)) {
  72. List<Log> logList=logService.findByUid(uid);
  73. request.setAttribute("logList", logList);
  74. return "success";
  75. }
  76. return "failed";
  77. }
  78.  
  79. public String modify () throws Exception {
  80. System.out.println("修改:"+log.toString());
  81. if (logService.modify(log)) {
  82. return "success";
  83. }
  84. return "failed";
  85. }
  86. }
  1. package com.sxl.actions;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.apache.struts2.ServletActionContext;
  6.  
  7. import com.opensymphony.xwork2.ActionSupport;
  8. import com.sxl.pojos.User;
  9. import com.sxl.services.LogService;
  10. import com.sxl.services.UserService;
  11.  
  12. public class UserAction extends ActionSupport {
  13. private User user;
  14. private String validatePass;
  15.  
  16. //提取出公共的部分
  17. private UserService userService=new UserService();
  18. HttpServletRequest request=ServletActionContext.getRequest();
  19.  
  20. public User getUser() {
  21. return user;
  22. }
  23. public void setUser(User user) {
  24. this.user = user;
  25. }
  26. public String getValidatePass() {
  27. return validatePass;
  28. }
  29. public void setValidatePass(String validatePass) {
  30. this.validatePass = validatePass;
  31. }
  32.  
  33. //用户登录
  34. public String login() throws Exception {
  35. int uid=userService.find(user);
  36. if (uid!=-1) {
  37. user.setUid(uid);
  38. request.getSession().setAttribute("user", user);
  39. return "personalCenter";
  40. }else {
  41. return "failed";
  42. }
  43. }
  44.  
  45. //添加用户
  46. public String add() throws Exception {
  47. if (!user.getPass().equals(validatePass)) {
  48. return "failed";
  49. }
  50. if (userService.add(user)) {
  51. return "personalCenter";
  52. }else {
  53. return "failed";
  54. }
  55. }
  56.  
  57. public String modify() throws Exception{
  58. if (!user.getPass().equals(validatePass)) {
  59. return "failed";
  60. }if (userService.modify(user)) {
  61. return "success";
  62. }else {
  63. return "failed";
  64. }
  65. }
  66. }

6、struts.xml 配置文件如下:

对应的代码:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4.  
  5. <package name="logsystem" namespace="/" extends="struts-default">
  6. <action name="user" class="com.sxl.actions.UserAction">
  7. <result name="personalCenter" type="dispatcher">/personalCenter.jsp</result>
  8. <result name="success" type="dispatcher">/success.jsp</result>
  9. <result name="failed" type="redirect">/failed.jsp</result>
  10. </action>
  11.  
  12. <action name="log" class="com.sxl.actions.LogAction">
  13. <result name="getAllLog" type="dispatcher">/getAllLog.jsp</result>
  14. <result name="success" type="dispatcher">/success.jsp</result>
  15. <result name="logModify" type="dispatcher">/logModify.jsp</result>
  16. <result name="failed" type="redirect">/failed.jsp</result>
  17. </action>
  18. </package>
  19. </struts>

& jsp s~

基于Struts2 的日志管理系统的Java实现的更多相关文章

  1. 基于struts2的学生报道管理系统(附github源码地址)

    本项目参考了<java web轻量级开发全体验>,加入了对mysql的支持. 一.基本业务功能 通过struts2框架,结合mysql数据库构建一个学生报到管理系统,来模拟学生报到登记的过 ...

  2. 基于SSH实现员工管理系统之框架整合篇

    本篇文章来源于:https://blog.csdn.net/zhang_ling_yun/article/details/77803178 以下内容来自慕课网的课程:基于SSH实现员工管理系统之框架整 ...

  3. 离线部署ELK+kafka日志管理系统【转】

    转自 离线部署ELK+kafka日志管理系统 - xiaoxiaozhou - 51CTO技术博客http://xiaoxiaozhou.blog.51cto.com/4681537/1854684 ...

  4. 日志管理系统ELK6.2.3

    https://www.jianshu.com/p/88f2cbedcc2a 写在前面 刚毕业工作的时候,处理日志喜欢自己写脚本抓取数据分析日志,然后在zabbix上展示出来.但是开发要看日志的时候, ...

  5. 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验

    一,使用Struts2标签库简化表单 在文章[基于Struts2框架实现登录案例]的基础上,通过使用Struts标签库可以简化登录页面login2.jsp <%@ page language=& ...

  6. 【课程分享】基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构、自己定义工作流)

    基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构.自己定义工作流) 课程讲师:张弘 课程分类:Java 适合人群:中级 课时数量:37课时 用到技术:Spring  ...

  7. Eventlog Analyzer日志管理系统、日志分析工具、日志服务器的功能及作用

    Eventlog Analyzer日志管理系统.日志分析工具.日志服务器的功能及作用 Eventlog Analyzer是用来分析和审计系统及事件日志的管理软件,能够对全网范围内的主机.服务器.网络设 ...

  8. 基于hive的日志分析系统

    转自 http://www.cppblog.com/koson/archive/2010/07/19/120773.html           hive 简介         hive 是一个基于  ...

  9. 基于UML的毕业设计管理系统的分析与设计

    基于UML的毕业设计管理系统的分析与设计 <本段与标题无关,自行略过 最近各种忙,天气不错,导师心情不错:“我们要写一个关于UML的专著”,一句话:“一个完整的系统贯穿整个UML的知识”:我:“ ...

随机推荐

  1. codevs 4909 寂寞的堆(写的好丑0.0)

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #defin ...

  2. 一些 Windows 系统不常见的 鼠标光标常数

    一些 Windows  系统不常见的 鼠标光标常数 Private Declare Function SetCursor Lib "user32" (ByVal hCursor A ...

  3. OJ常见问题及必须认识的对拍处理水题

    HDUOJ: 常见问题及解答 Q: Online Judge(以下简称OJ)支持哪些语言? A: 目前为止,HDOJ支持C.C++.Pascal和Java四种语言. Q: 有什么条件判断我的程序是在O ...

  4. [转帖]了解AmbiLight知识

    了解科技前沿的方法..American Online=AOL.algorithm算法.(Denzel say, and I don't know) Engadget瘾科技网站.英文版Engadget网 ...

  5. 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 ...

  6. 【OpenSSL】创建证书

    [-] 1生成根证书 1 生成RSA私钥 2 生成证书请求 3 签发自签名证书 2 生成用户证书 1 生成RSA私钥 2 生成证书请求 3 签发证书   1)生成根证书 1.1) 生成RSA私钥 op ...

  7. 关于SVD(Singular Value Decomposition)的那些事儿

    SVD简介 SVD不仅是一个数学问题,在机器学习领域,有相当多的应用与奇异值都可以扯上关系,比如做feature reduction的PCA,做数据压缩(以图像压缩为代表)的算法,还有做搜索引擎语义层 ...

  8. Hibernate 性能优化之二级缓存

    二级缓存是一个共享缓存,在二级缓存中存放的数据是共享数据特性     修改不能特别频繁     数据可以公开二级缓存在sessionFactory中,因为sessionFactory本身是线程安全,所 ...

  9. text-overflow:ellipsis 的应用(转载)

    关键字: text-overflow:ellipsis 语法:text-overflow : clip | ellipsis 取值: clip :默认值 .不显示省略标记(...),而是简单的裁切. ...

  10. Thinkphp 零散知识点(caa/js路径,引入第三方类,ajax返回,session/cookie)

    一.关于JS和CSS路径问题 1.找路径是从入口文件index.php来找的,而不是从文件本身所在位置来找, 因为我们访问时是访问的入口文件 2.在存放JS和CSS的时候可以放到public文件夹下 ...