Home>

I want to get the set value using DTO
Look at the comments in //.

Error message
java.sql.SQLIntegrityConstraintViolationException: Column 'user_id' cannot be null
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:117)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException (SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal (ClientPreparedStatement.java:955)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal (ClientPreparedStatement.java:1094)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal (ClientPreparedStatement.java:1042)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate (ClientPreparedStatement.java:1345)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate (ClientPreparedStatement.java:1027)
    at DB.DAO.regAttendance (DAO.java:180)
    at login.AttendanceInfomation.doPost (AttendanceInfomation.java:60)
    at javax.servlet.http.HttpServlet.service (HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service (HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process (AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol $AbstractConnectionHandler.process (AbstractProtocol.java:684)
Applicable source code
/**
 * Servlet implementation class AttendanceInfomation
 * /
@WebServlet ("/ AttendanceInfo")
public class AttendanceInfomation extends HttpServlet {
    private static final long serialVersionUID = 1L;
    / **
     * @see HttpServlet # HttpServlet ()
     * /
    public AttendanceInfomation () {
        super ();
        // TODO Auto-generated constructor stub
    }
    / **
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse
     * response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {// TODO Auto-generated method stub
        response.getWriter (). append ("Served at:") .append (request.getContextPath ());
    }
    / **
     * @see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse
     * response)
     * /
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        DAO dao = new DAO ();
        DTO dto = new DTO ();
        dao.regAttendance (dto.getUserid ());
    }
}
public class DAO {
Private static Connection getConnection () {
Final String DSN = "*";//
Final string USER = "*";
Final string PASSWORD = "*";
Try try {
, Class.forName ("com.mysql.cj.jdbc.Driver");
             return DriverManager.getConnection (DSN, USER, PASSWORD);
} Catch (Exception e) {
             throw new IllegalArgumentException (e);
}
}
Private static void allClose (PreparedStatement statement, Connection connection) {
, If (statement! = Null) {
, Try try {
                        statement.close ();
, C} catch (SQLException e) {
                        e.printStackTrace ();
...}
\}
                if (connection! = null) {
                    try {
                        connection.close ();
                    } catch (SQLException e) {
                        e.printStackTrace ();
                    }
                }
            }
         static Connection connection = null;
            static PreparedStatement statement = null;

public DTO loginUser (String id, String password) {
    DTO dto = new DTO ();
    try {
        connection = getConnection ();
Statement = connection.prepareStatement ("SELECT * FROM login WHERE id =? And password =?");
Statement.setString (1, id);Statement.setString (2, password);
ResultSet resultSet = statement.executeQuery ();
    if (! resultSet.next ()) {
             return null;
}
Else {
              // I want to get this set value with regAttendance
Dto.setUserid (id);
}
}} Catch (SQLException e) {
    E.printStackTrace ();
} Finally {
, AllClose (statement, connection);
}
    return dto;
}
public int regAttendance (String id) {
Int result = 0;
Try try {
Connection = getConnection ();
Statement = connection.prepareStatement ("INSERT INTO time_card (user_id, date, attendance_time) VALUES (?, Now (), now ())");
Statement.setString (1, id);
Result = statement.executeUpdate ();
}} Catch (SQLException e) {
E.printStackTrace ();
} Finally {
, AllClose (statement, connection);
}
        return result;
public class DTO {
    private String userid;
    private String password;
    public String getUserid () {
        return userid;
    }
    public String getPassword () {
        return password;
    }

    public void setUserid (String userid) {
        this.userid = userid;
    }
    public void setPassword (String password) {
        this.password = password;
    }
}

A DTO instance was created in the DAO global variable and set there.

Supplemental information (FW/tool version etc.)

Eclipse

  • Answer # 1

    public DTO loginUser (String id, String password) {
        DTO dto = new DTO ();
    ~ About ~
    Dto.setUserid (id);
    ~ About ~
        return dto;
    }
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
            DAO dao = new DAO ();
            DTO dto = new DTO ();
            dao.regAttendance (dto.getUserid ());
        }


    I'm new to dto with loginUser () and set the value with setUserid, but the code
    Doto () uses a different dto (uses something different from the dto generated by loginUser ())
    Is the cause.
    Since it is unknown when loginUser () is called, the code itself cannot be modified, but
    Since dto is returned by loginUser (), I think that it is natural to hold it in session.

    For comments
      

    Is it easier to add a program that uses loginUser ()?

    It's not essential if you just want to fix the problem you are experiencing.
    However, depending on the respondent, you may be pointed out that more time is required to read and understand the code.
    (For example, in this example, you need to guess that loginUser () is called elsewhere before this problem occurs.)
    I personally think that I should put it when I receive a postscript request.

      

    I want to get dto of loginUser () even outside this program ...

    If it is

    , it seems that the use of session is more appropriate.