我可以创建一个学生,但它不会保存在 mysql 表上。我收到一个控制台错误,我可以弄清楚它是啥[关闭]

Posted

技术标签:

【中文标题】我可以创建一个学生,但它不会保存在 mysql 表上。我收到一个控制台错误,我可以弄清楚它是啥[关闭]【英文标题】:I can create a student but it doesn't save on mysql table. i get a console error that i can figure it out what it is [closed]我可以创建一个学生,但它不会保存在 mysql 表上。我收到一个控制台错误,我可以弄清楚它是什么[关闭] 【发布时间】:2013-10-06 11:58:50 【问题描述】:

这是我的 java 类(dao):

public void crearAlumno(String nombre,String apellido, int dni, 
                        int edad,int date,int month,
                        int year,String curso,int legajo)
    Alumno alumno = new Alumno(nombre, apellido, dni, 
                               edad, date, month, 
                               year, curso,legajo);
    java.util.Date utilDate = alumno.getFechaNC();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    //bd
    String url = "jdbc:mysql://localhost/captton";
    String user = "root";
    String password = "";

    Connection comn = null;
    PreparedStatement start = null;

    try
        comn = DriverManager.getConnection(url, user, password);
        start = comn.prepareStatement("INSERT INTO alumnos(legajo,nombre,apellido,curso,dni,edad,fechanc) "
                + "VALUES(?,?,?,?,?,?)");
        start.setInt(1, alumno.getLegajo());
        start.setString(2, alumno.getNombre());
        start.setString(3, alumno.getApellido());
        start.setString(4, alumno.getCurso());
        start.setInt(5, alumno.getDni());
        start.setInt(6, alumno.getEdad());
        start.setDate(7, sqlDate);
        start.executeUpdate();
    catch (SQLException e)
        e.printStackTrace();
     finally
        comn.close();
    catch (SQLException e)
        e.printStackTrace();
    

这是错误:

java.sql.SQLException: Parameter index out of range (7 > number of parameters, which is 6).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3288)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3272)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3314)
    at com.mysql.jdbc.PreparedStatement.setDate(PreparedStatement.java:3190)
    at com.mysql.jdbc.PreparedStatement.setDate(PreparedStatement.java:3159)
    at foo.dao.AlumnoDao.crearAlumno(AlumnoDao.java:57)
    at foo.sistemagestion.SistemaGestionController.accionAgregarAlumnos(SistemaGestionController.java:95)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:139)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

【问题讨论】:

嗯,您显示 6 个占位符 (?) 但有 7 个参数。错误信息很清楚。 错误信息是为了帮助你,而不是完全被忽视。 【参考方案1】:

数一数你的陈述中的列!有七列,但有六个值:

INSERT INTO alumnos(legajo,nombre,apellido,curso,dni,edad,fechanc) "
          + "VALUES(?,?,?,?,?,?)");

你很容易忘记问号(列占位符)试试这个:

INSERT INTO alumnos(legajo,nombre,apellido,curso,dni,edad,fechanc) "
          + "VALUES(?,?,?,?,?,?,?)");

【讨论】:

【参考方案2】:

您在值中缺少一个问号

start = comn.prepareStatement("INSERT INTO alumnos(legajo,nombre,apellido,curso,dni,edad,fechanc) "
              + "VALUES(?,?,?,?,?,?,?)");

【讨论】:

【参考方案3】:

在查询字符串中再添加一个占位符?

start = comn.prepareStatement("INSERT INTO alumnos(legajo,nombre,apellido,curso,dni,edad,fechanc) "
          + "VALUES(?,?,?,?,?,?,?)");

【讨论】:

以上是关于我可以创建一个学生,但它不会保存在 mysql 表上。我收到一个控制台错误,我可以弄清楚它是啥[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

您可以在 PHP/MySQL 数据库表的每一行中创建/插入表吗?

2_MySQL

mysql创建表

Java保存功能不会覆盖

MySQL 存储过程,获取使用游标查询的结果集

MySQL 无法使用表 wp_users 创建外键引用