⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginaction.java

📁 这是 一个 考试测试系统。哈哈 个 考试测试系统。哈哈
💻 JAVA
字号:
package com.edgedu.struts.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.edgedu.connection.GetConn;
import com.edgedu.struts.form.EmployeeForm;

//用户登陆Action
public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException {

		// 取得表单内容
		EmployeeForm loginf = (EmployeeForm) form;
		// 用于保存数据库中用户密码的字符串

		// 用表单中的用户名查数据库
		String password = "";
		String employeeId = "";
		String employeeName = "";
		String trueName = "";

		String sql = "select *  from employee where employeeName='"
				+ loginf.getName() + "'";
		Statement stmt = null;
		ResultSet rs = null;
		Connection conn = null;

		try {
			conn = new GetConn().getConn();
			stmt = conn.createStatement();
		} catch (SQLException e) {
			System.out.println("连接数据库出错!");
		}

		if (stmt != null) {
			try {
				rs = stmt.executeQuery(sql);
				while (rs.next()) {
					trueName = rs.getString("trueName");
					employeeId = rs.getString("employeeId");
					employeeName = rs.getString("employeeName");
					password = rs.getString("employeepassword");

				}
				
				request.getSession().setAttribute("employeeId", employeeId);
				request.getSession().setAttribute("employeeName", employeeName);
				request.getSession().setAttribute("trueName", trueName);
				request.getSession().setAttribute("password", password);
				
			} catch (SQLException e) {
				System.out.println("执行SQL与句出错!");
			}

		} else {
			try {
				stmt = new GetConn().getConn().createStatement();
			} catch (SQLException e) {
				System.out.println("连接数据库出错!");
			}
		}

		// 对比密码
		if (!"".equals(loginf.getPassword())) {
			if (password.equals(loginf.getPassword())) {
				// 登陆成功,既取得了密码,并匹配

				return (mapping.findForward("ViewEmployee"));
			} else {
				// 登陆失败
				return (mapping.findForward("login"));
			}
		}
		// 关闭连接
		rs.close();
		stmt.close();
		new GetConn().closConn(conn);
		return (mapping.findForward("login"));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -