繁体   English   中英

为什么我得到更新用户设置名称 = ** 未指定 ** 错误?

[英]Why i get update users set name = ** NOT SPECIFIED ** error?

Why i get com.mysql.cj.jdbc.ClientPreparedStatement: update users set name = ** NOT SPECIFIED **,email= ** NOT SPECIFIED **, country = ** NOT SPECIFIED ** where id = ** NOT SPECIFIED * *; 错误?

My UserDao Class
package com.xadmin.usermanagement.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xadmin.usermanagement.bean.User;

public class UserDao {
    //Connection information
private String jdbcURL = "jdbc:mysql://localhost:3306/userdb?useSSL=false";
private String jdbcUsername = "root";
private String jdbcPassword = "M123456789!v";
private String jdbcDriver = "com.mysql.cj.jdbc.Driver";
//Sql query information
private static final String INSERT_USERS_SQL = "INSERT INTO users" + "  (name, email, country) VALUES "
        + " (?, ?, ?);";
private static final String SELECT_USER_BY_ID = "select id,name,email,country from users where id = ?;";
private static final String SELECT_ALL_USERS = "select * from users;";
private static final String DELETE_USERS_SQL = "delete from users where id = ?;";
private static final String UPDATE_USERS_SQL = "update users set name = ? ,email= ?, country = ? where id = ? ;";
public UserDao() {
}
//Create database connection
protected Connection getConnection() {
    Connection connection = null;
    try {
        Class.forName(jdbcDriver);
        connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
        
    }catch(Exception e) {
        e.printStackTrace();
    }
    return connection;
    
}
//insert User
public void insertUser(User user) {
    System.out.println(INSERT_USERS_SQL);
    try(Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL);){
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getEmail());
            preparedStatement.setString(3, user.getCountry());
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
    }catch(SQLException e) {
        printSQLException(e);
    }
}
//Select User by Id
public User selectUser(int id) {
    User user = null;
    //Establishing connection
    try(Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(SELECT_USER_BY_ID);){
        //We added 1 value which come from id parameter
        preparedStatement.setInt(1, id);
        System.out.println(preparedStatement);
        //We executed SELECT_USER_BY_ID query with executeQuery() method
        // executeQuery() method returns ResultSet object and we attached to ResultSet reference
        ResultSet rs = preparedStatement.executeQuery();
        //In while loop we say next method for iterating values
        while(rs.next()) {
            String name = rs.getString("name");
            String email = rs.getString("email");
            String country = rs.getString("country");
            //After iterating values we change 'null' value to constructor values
            user = new User(name, email, country);
        }
    }catch(SQLException e) {
        printSQLException(e);
    }
    // For not void method ,we should return value.
    return user;
}
//Select all users
public List<User> selectAllUsers(){
    List<User> users = new ArrayList<User>();
    try(Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_USERS);){
            System.out.println(preparedStatement);
            ResultSet rs = preparedStatement.executeQuery();
            while(rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String email = rs.getString("email");
                String country = rs.getString("country");
                users.add(new User(id, name, email, country));
            }
    }catch(SQLException e) {
        printSQLException(e);
    }
    return users;
}
public boolean updateUser(User user) throws SQLException {
    boolean rowUpdated;
    try(Connection connection = getConnection();
            PreparedStatement statement = connection.prepareStatement(UPDATE_USERS_SQL);){
            System.out.println("Updated user: " + statement);
            statement.setString(1, user.getName());
            statement.setString(2, user.getEmail());
            statement.setString(3, user.getCountry());
            statement.setInt(4, user.getId());
            rowUpdated = statement.executeUpdate() > 0;
    }
    return rowUpdated;
}

public boolean deleteUser(int id) throws SQLException{
    boolean rowDeleted;
    try(Connection connection = getConnection();
            PreparedStatement statement = connection.prepareStatement(DELETE_USERS_SQL);){
                statement.setInt(1, id);
                rowDeleted = statement.executeUpdate()>0;
            }
    return rowDeleted;
}


//Sql exception for all errors
private void printSQLException(SQLException ex) {
    for(Throwable e:ex) {
        if(e instanceof SQLException) {
            e.printStackTrace(System.err);
            System.err.println("SQLState: " + ((SQLException) e).getSQLState());
            System.err.println("Error code: " + ((SQLException) e).getErrorCode());
            System.err.println("Message: " + e.getMessage());
            Throwable t = ex.getCause();
            while(t!=null) {
                t = t.getCause();
            }
        }
    }
}

}

它是 servlet 和 jsp 项目这是我的 Servlet class

package com.xadmin.usermanagement.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
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 com.xadmin.usermanagement.bean.User;
import com.xadmin.usermanagement.dao.UserDao;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/")
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserDao userDao;

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

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init() throws ServletException {
        userDao = new UserDao();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getServletPath();
        switch (action) {
        case "/new":
            try {
                showNewForm(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }

            break;
        case "/insert":
            try {
                insertUser(request, response);
            } catch (Exception e) {
                // TODO: handle exception
            }

            break;
        case "/delete":
            try {
                deleteUser(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }

            break;
        case "/edit":
            try {
                showEditForm(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }

            break;
        case "/update":
            
            try {
                updateUser(request, response);
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        
        
            break;
        default:
            try {
                listUser(request, response);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispacther = request.getRequestDispatcher("user-form.jsp");
        dispacther.forward(request, response);

    }

    private void insertUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String country = request.getParameter("country");
        User newUser = new User(name, email, country);
        userDao.insertUser(newUser);
        response.sendRedirect("list");
    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        try {
            userDao.deleteUser(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("list");
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        User existingUser = userDao.selectUser(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
        request.setAttribute("user", existingUser);
        dispatcher.forward(request, response);

    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String country = request.getParameter("country");
        User book = new User(id, name, email, country);
        userDao.updateUser(book);
        response.sendRedirect("list");
    }

    private void listUser(HttpServletRequest request, HttpServletResponse response)
            throws IOException, SQLException, ServletException {
        try {
            List<User> listUser = userDao.selectAllUsers();
            request.setAttribute("listUser", listUser);
            RequestDispatcher dispatcher = request.getRequestDispatcher("user-list.jsp");
            dispatcher.forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

这是我的豆子 class

package com.xadmin.usermanagement.bean;

public class User {
private int id;
private String name;
private String email;
private String country;
public User(String name, String email, String country) {
    super();
    this.name = name;
    this.email = email;
    this.country = country;
}
public User(int id, String name, String email, String country) {
    super();
    this.id = id;
    this.name = name;
    this.email = email;
    this.country = country;
}
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 getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
}

我不知道是什么问题请帮我解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM