|
3Java金币
这是我的 数据表,我想取出grade列里的值放在session中,以供我做用户分级
但是我取出的都是username中的值
我该怎么做才能取到grade中的值?
还有如果我在session中放入多个值(如同时放入id,username,grade),该如何分别取出?
代码如下:
userService
- import java.sql.ResultSet;
- import java.util.ArrayList;
-
- import com.qym.Utils.SqlHelper;
- import com.qym.DAO.userDAO;
-
- public class userService {
-
- public boolean checkUser(userDAO user){
-
-
- String sql="select * from user where username=? and passwd=?";
- String paras[]={user.getName()+"",user.getPassword()};
- ArrayList al=new SqlHelper().executeQuery(sql, paras);
-
- if(al.size()==0){
- return false;
- }else {
-
- // for(int i=0;i<al.size();i++){
- //
- // }
-
-
- Object []objects=(Object[]) al.get(0);
-
- //user.setName((String)objects[1]);
- user.setGrade(Integer.parseInt(objects[4].toString()));
-
- return true;
- }
-
- }
-
-
-
-
- public boolean checkAddUser(userDAO user){
-
-
-
-
- String sql="insert into user(username,passwd) values(?,?)";
- String a=user.getName();
- String b=user.getPassword();
- String[] parameters={a,b};
- SqlHelper.executeUpdate(sql, parameters);
-
-
-
-
- return true;
-
-
- }
- }
复制代码
GoMyJspUI
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.qym.DAO.userDAO;
- import com.qym.Service.userService;
-
- public class GoMyJspUI extends HttpServlet {
-
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
-
-
- String name=request.getParameter("name");
- String p=request.getParameter("password");
-
-
- userDAO loginuser=new userDAO(name,p);
- userService userService=new userService();
-
-
-
-
- if(userService.checkUser(loginuser)){
-
-
- String s="mySession";
- request.getSession().setAttribute("MS", s);
- System.out.print(request.getSession().getAttribute("MS"));
-
-
-
- request.getSession().setAttribute("loginuser", loginuser);
- System.out.println("属性值已经保存在session");
-
- Integer a =Integer.parseInt( request.getSession().getAttribute("loginuser").toString());
- //String a =( request.getSession().getAttribute("loginuser").toString());
- System.out.println(a);
-
-
- request.getRequestDispatcher("/WEB-INF/MyJsp.jsp").forward(request, response);
- }
- else {
- request.getRequestDispatcher("/WEB-INF/ShiBai.jsp").forward(request, response);
- }
- }
-
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- this.doGet(request, response);
-
- }
-
-
-
- }
复制代码
userDAO
- public class userDAO {
-
- private int id;
- private String name;
- private String password;
- private int grade;
-
- public userDAO(String name, String password){
- super();
- this.name=name;
- this.password=password;
- }
-
- public int getGrade() {
- return grade;
- }
-
- public void setGrade(int grade) {
- this.grade = grade;
- }
-
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String toString(){
- return name;
- }
-
-
- }
复制代码
|
|