前提・実現したいこと
DTOを使ってsetした値をgetしたい
//でコメントしているところを見てください。
発生している問題・エラーメッセージ
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)
該当のソースコード
Java
1/** 2 * Servlet implementation class AttendanceInfomation 3 */ 4@WebServlet("/AttendanceInfo") 5public class AttendanceInfomation extends HttpServlet { 6 private static final long serialVersionUID = 1L; 7 8 /** 9 * @see HttpServlet#HttpServlet() 10 */ 11 public AttendanceInfomation() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 16 /** 17 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 18 * response) 19 */ 20 protected void doGet(HttpServletRequest request, HttpServletResponse response) 21 throws ServletException, IOException { 22 // TODO Auto-generated method stub 23 response.getWriter().append("Served at: ").append(request.getContextPath()); 24 } 25 26 /** 27 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 28 * response) 29 */ 30 protected void doPost(HttpServletRequest request, HttpServletResponse response) 31 throws ServletException, IOException { 32 // TODO Auto-generated method stub 33 DAO dao = new DAO(); 34 35 DTO dto = new DTO(); 36 37 dao.regAttendance(dto.getUserid()); 38 } 39 40} 41
java
1public class DAO { 2 private static Connection getConnection() { 3 final String DSN = "*";// 4 final String USER = "*"; 5 final String PASSWORD = "*"; 6 7 try { 8 Class.forName("com.mysql.cj.jdbc.Driver"); 9 return DriverManager.getConnection(DSN,USER,PASSWORD); 10 } catch (Exception e) { 11 throw new IllegalArgumentException(e); 12 } 13 } 14 15 private static void allClose(PreparedStatement statement, Connection connection) { 16 if (statement != null) { 17 try { 18 statement.close(); 19 } catch (SQLException e) { 20 e.printStackTrace(); 21 } 22 } 23 if (connection != null) { 24 try { 25 connection.close(); 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 } 32 static Connection connection = null; 33 static PreparedStatement statement = null; 34 35 36public DTO loginUser(String id,String password) { 37 DTO dto = new DTO(); 38 39 try { 40 connection = getConnection(); 41 42 statement = connection.prepareStatement("SELECT * FROM login WHERE id = ? and password = ?"); 43 statement.setString(1, id); 44 statement.setString(2, password); 45 ResultSet resultSet = statement.executeQuery(); 46 47 if (!resultSet.next()) { 48 return null; 49 } 50 else { 51 //このsetした値をregAttendanceでgetしたい 52 dto.setUserid(id); 53 } 54 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } finally { 58 allClose(statement, connection); 59 } 60 return dto; 61} 62 63public int regAttendance(String id) { 64 int result = 0; 65 try { 66 connection = getConnection(); 67 68 statement = connection.prepareStatement("INSERT INTO time_card(user_id,date,attendance_time) VALUES (?, now(), now())"); 69 statement.setString(1, id); 70 result = statement.executeUpdate(); 71 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } finally { 75 allClose(statement, connection); 76 } 77 return result; 78
Java
1public class DTO { 2 private String userid; 3 private String password; 4 5 public String getUserid() { 6 return userid; 7 } 8 public String getPassword() { 9 return password; 10 } 11 12 13 public void setUserid(String userid) { 14 this.userid = userid; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 20}
試したこと
DAOのグローバル変数にDTOのインスタンスを作りそこにsetした。
補足情報(FW/ツールのバージョンなど)
Eclipse
回答1件
あなたの回答
tips
プレビュー