com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(

时间:2018-03-22 05:17:24

标签: java mysql jdbc

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(storechainmanagementuser,CONSTRAINT user_ibfk_1 FOREIGN KEY( store_id)参考storestore_id))sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)at sun.reflect.DelegatingConstructorAccessorImpl com.mysql.jdbc.Util.getInstance(Util。)中的com.mysql.jdbc.Util.handleNewInstance(Util.java:425)中java.lang.reflect.Constructor.newInstance(未知来源)的.newInstance(未知来源)。的java:408)在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)在com.mysql.jdbc.MysqlIO.checkErrorPacket(了MysqlIO .com:3909)com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)at com.mysql.jdbc.ConnectionImpl.execSQL( ConnectionImpl.java:2484)在com.mysql.jd bc.PreparedStatement.executeInternal(PreparedStatement.java:1858)在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)在com.mysql .jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)at com.azilen.storemanagement.dao.StoreManagementDaoImpl.saveManager(StoreManagementDaoImpl.java:323)at at com.azilen.storemanagement.service.StoreManagementServiceImpl.addManager(StoreManagementServiceImpl.java:35)在com.azilen.storemanagement.controller.CreateUserServlet.doPost(CreateUserServlet.java:51)在javax.servlet.http.HttpServlet.service(的HttpServlet。 java:648)javax.servlet.http.HttpServlet.service(HttpServlet.java:729)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)at org.apache.catalina.core.ApplicationFilterChain .doFilter(A pplicationFilterChain.java:207)在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)在org.apache .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java) :106)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)org.apache.catalina.valves。位于org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)的orr.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)的orde.Verve.invoke(ErrorReportValve.java:79)中的ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.coyot上的.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) e.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:672)at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun( NioEndpoint.java:1520)在java.util.concurrent的java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)的org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1476)。 ThreadPoolExecutor $ Worker.run(未知来源)atg.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Unknown Source)

@Override
public int saveManager(Manager manager) throws SQLException {
    int status = 0;
    PreparedStatement ps = null;
    Connection con = null;
    try {
        con = getConnection();
        ps = con.prepareStatement(
                "insert into user(user_id,first_name,last_name,email,password,mobile_number,user_type,store_id) values (?,?,?,?,?,?,?,?)");
        ps.setInt(1, (int) manager.getUserId());
        ps.setString(2, manager.getFname());
        ps.setString(3, manager.getLname());
        ps.setString(4, manager.getEmail());
        ps.setString(5, manager.getPassword());
        ps.setString(6, manager.getPhone());
        ps.setString(7, manager.getUserType());
        ps.setInt(8, (int) manager.getStore().getStoreId());
        status = ps.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
    }
    return status;
}

0 个答案:

没有答案