程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 服務器-求大神幫助解決小問題,一直無法調用到DAO層的login方法

服務器-求大神幫助解決小問題,一直無法調用到DAO層的login方法

編輯:編程解疑
求大神幫助解決小問題,一直無法調用到DAO層的login方法

package com.tnt.jsp;

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 javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;

import entity.Users;
import Dao.UserDao;

/**

  • Servlet implementation class LoginServlet
    */
    public class LoginJsonServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet() */ public LoginJsonServlet() { super(); // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    •  response)
      

      */
      protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
      request.setCharacterEncoding("UTF-8");
      // response.setContentType("Textcml;charset=UTF-8");
      String uname = request.getParameter("uname");
      String upass = request.getParameter("upass");
      // PrintWriter pw = response.getWriter();

      Users condition = new Users();
      condition.setUname(uname);
      condition.setUpass(upass);

      UserDao userDao = new UserDao();
      Users user = userDao.login(condition)就是這裡一直無法調用????????

      PrintWriter out = response.getWriter();
      if (user != null) {//生成json字符串2
      ObjectMapper mapper = new ObjectMapper();
      String json = mapper.writeValueAsString(user);
      out.write(json);
      out.flush();
      out.close();
      }

      // 得到session對象
      // 如果傳false,當前session對象就返回session對象,如果不存在就返回新創建的對象
      HttpSession session = request.getSession();

      if (user != null) {
      session.setAttribute("USER", user);
      request.setAttribute("USERNAME", uname);
      //請求轉發
      request.getRequestDispatcher("/success.jsp ").forward(request,response);
      // 重定向
      response.sendRedirect("success.html");
      request.getRequestDispatcher("/BloggersSearvlet").forward(request,
      response);
      System.out.println("login");

       response.sendRedirect("BloggersSearvlet");
      

      // pw.println(uname + "登陸成功!");
      } else {
      // pw.println(uname + "登錄失敗");
      request.getRequestDispatcher("/fail.jsp").forward(request,response);
      request.getRequestDispatcher("/fail.jsp")
      .forward(request, response);
      }

    }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    • response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); doGet(request, response); }

}

DAO類
package Dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.locks.Condition;

import org.apache.tomcat.jni.User;

import entity.Users;

public class UserDao {
public Users login(User condition)

{
    Connection connection = ConnectionManager.getConnection();
    Statement stmt = null;
    Users user = null;
    String sql = "select  userid,username,password from tbl_user where username='"
            + user.getUname()
            + "' and password ='"
            + user.getUpass()
            + "'";

    ResultSet rs = null;
    try {
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            user = new Users();
            int userid = rs.getInt("USERID");
            String uname = rs.getString("UNAME");
            String upass = rs.getString("UPASS");
            int age = rs.getInt("AGE");
            user.setUserid(userid);
            user.setUname(uname);
            user.setUpass(upass);
            user.setAge(age);

        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeResultSet(rs);
        ConnectionManager.closeStatement(stmt);
        ConnectionManager.closeConnection(connection);
    }
    return user;

}

}

最佳回答:


我看出來了:

 Users condition = new Users();
condition.setUname(uname);
condition.setUpass(upass);
UserDao userDao = new UserDao();
Users user = userDao.login(condition)就是這裡一直無法調用????????

這裡的userDao.login(condition);你的condition類型時Users,而你的dao層裡面login方法的參數:是import org.apache.tomcat.jni.User;

 import org.apache.tomcat.jni.User;
import entity.Users;
public class UserDao {
public Users login(User condition)

類型出錯了,把你login方法裡的User改成Users就可以了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved