package com.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.common.DBcon;
import com.dto.depDto;
public class login {
private String username;
private String password;
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// 登陆
public String loginConfirm() throws Exception {
Connection conn = DBcon.getConnection();
Statement stmt = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
try {
stmt = conn.createStatement();
String sql1 = "select * from userinfomation where username='"
+ this.username + "'";
rs1 = stmt.executeQuery(sql1);
if (!rs1.next()) {
this.msg = "用户名不正确!";
return "Error";
} else {
String sql2 = "select * from userinfomation where password='"
+ this.password + "'";
rs2 = stmt.executeQuery(sql2);
if (!rs2.next()) {
this.msg = "密码不正确!";
return "Error";
}
}
rs1.close();
rs2.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(stmt != null) {
stmt.close();
stmt = null;
}
if( conn!=null) {
conn.close();
stmt = null;
}
}
return queryAll();
}
public String queryAll() throws Exception {
Connection conn = DBcon.getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
String sql = "select * from dep";
ResultSet rs = stmt.executeQuery(sql);
List<depDto> list = new ArrayList<depDto>();
while (rs.next()) {
depDto dt = new depDto();
dt.setId(rs.getInt("id"));
dt.setName(rs.getString("name"));
dt.setSex(rs.getString("sex"));
dt.setTime(rs.getString("time"));
list.add(dt);
}
HttpSession session = ServletActionContext.getRequest()
.getSession();
session.setAttribute("listAll", list);
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(stmt != null) {
stmt.close();
stmt = null;
}
if( conn!=null) {
conn.close();
conn = null;
}
}
return "showAllInfo";
}
public String add() throws Exception {
String name = ServletActionContext.getRequest().getParameter("name");
String sex = ServletActionContext.getRequest().getParameter("sex");
java.util.Date date = new java.util.Date();
String time = "";
SimpleDateFormat s = new SimpleDateFormat("yyyy年-MM月dd日-HH:mm:ss");
time = s.format(date);
Connection conn = DBcon.getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
String sql = "insert into dep(name,sex,time) values('" + name
+ "','" + sex + "','" + time + "')";
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.close();
conn = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
}
return queryAll();
}
public String del() throws Exception {
String id = ServletActionContext.getRequest().getParameter("id");
int i = Integer.parseInt(id);
Connection conn = DBcon.getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
String sql = "delete from dep where id=" + i;
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.close();
conn = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
}
return queryAll();
}
public String update() throws Exception {
String id = ServletActionContext.getRequest().getParameter("id");
int i = Integer.parseInt(id);
Connection conn = DBcon.getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
String sql = "select * from dep where id=" + i;
ResultSet rs = stmt.executeQuery(sql);
List<depDto> list = new ArrayList<depDto>();
while (rs.next()) {
depDto dt = new depDto();
dt.setId(rs.getInt("id"));
dt.setName(rs.getString("name"));
dt.setSex(rs.getString("sex"));
dt.setTime(rs.getString("time"));
list.add(dt);
}
HttpSession session = ServletActionContext.getRequest()
.getSession();
session.setAttribute("list", list);
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.close();
conn = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
}
return "update";
}
public String update1() throws Exception {
String sex = ServletActionContext.getRequest().getParameter("sex");
String id = ServletActionContext.getRequest().getParameter("id");
int i = Integer.parseInt(id);
Connection conn = DBcon.getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
String sql = "update dep set sex='" + sex + "' where id=" + i;
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.close();
conn = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
}
return queryAll();
}
public String IdSelect() throws Exception{
String id = ServletActionContext.getRequest().getParameter("id");
int i = Integer.parseInt(id);
Connection conn = DBcon.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dep where id=" + i + "");
List<depDto> list = new ArrayList<depDto>();
if (rs.next()) {
depDto dt = new depDto();
dt.setId(rs.getInt("id"));
dt.setName(rs.getString("name"));
dt.setSex(rs.getString("sex"));
dt.setTime(rs.getString("time"));
list.add(dt);
} else{
return "errorInfo";
}
HttpSession session = ServletActionContext.getRequest()
.getSession();
session.setAttribute("listId", list);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.close();
conn = null;
}
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
}
return "IdSelect";
}
public String NameSelect() throws Exception{
String name = ServletActionContext.getRequest().getParameter("name");
Connection conn = DBcon.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dep where name='" + name + "'");
List<depDto> list = new ArrayList<depDto>();
while (rs.next()) {
depDto dt = new depDto();
dt.setId(rs.getInt("id"));
dt.setName(rs.getString("name"));
dt.setSex(rs.getString("sex"));
dt.setTime(rs.getString("time"));
list.add(dt);
}
HttpSession session = ServletActionContext.getRequest()
.getSession();
session.setAttribute("listName", list);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.close();
conn = null;
}
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
}
return "NameSelect";
}
public String SexSelect() throws Exception{
String sex = ServletActionContext