10

报错:HTTP Status 500 – Internal Server Error

用的druid连接池,5.7.31的mysql,8.5.60的tomcat,1.8的Jdk.数据库部分User,UserDao和JDBCUtil写完测试了,功能可以。Servlet啥功能也没有的时候是可以访问的,但是一旦用到UserDao的功能就报错。查了很多事jar包目录的问题,我的jar包就在WEB-INF目录下实在找不出原因了。多谢有缘人。

报错页面:

出错部分:

UserDao ud = new UserDao();
User user = ud.Login(loginuser);

全部代码:

package com.xupt.domain;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}


import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());

    public User Login(User loginuser){
        String sql = "select * from useraccount where username = 'liu' and password = '123456'";

        /*User user = template.queryForObject(sql,
                new BeanPropertyRowMapper<User>(User.class),
                loginuser.getUsername(),loginuser.getPassword());*/
        User user = template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class));

        return user;
    }
}


package com.xupt.Util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.sun.corba.se.spi.orbutil.fsm.Input;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtil {
    private static DataSource ds;

    static {
        try {
            Properties pro = new Properties();

            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");

            pro.load(is);

            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource(){
        return ds;
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}


package com.xupt.servlet;

import com.xupt.dao.UserDao;
import com.xupt.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ServletLogin")
public class ServletLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hello");
        String username = "liu";
        String password = "123456";

        User loginuser = new User();
        loginuser.setUsername("username");
        loginuser.setPassword("password");

        UserDao ud = new UserDao();
        User user = ud.Login(loginuser);
        System.out.println(user);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

查看全部
weixin_43457559
哦糟了
2020/12/03 13:47
  • spring
  • java
  • tomcat
  • jar
  • java-ee
  • 点赞
  • 收藏
  • 已关闭
    私信
满意答案
查看全部

14个回复