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~
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;
}
}