【问题标题】:java.sql.SQLException: Column 'countryid' not foundjava.sql.SQLException:找不到列“countryid”
【发布时间】:2018-05-05 12:25:24
【问题描述】:

这是我的 java 方法,我使用它来更新用户
在后端我使用 jdbc 而不是 jdbc 模板来更新用户 我无法插入 countryid,因为错误显示未找到 countryid 列名。错误显示缺少国家 ID,但在我的 sql 编辑器中已经看到查询对级别正确

 public User updateUsers(int id) throws SQLException {
    User user = null;
    Connection con = null;
    PreparedStatement ps = null;
    int active = 0;
    try {
        con = createConnection();
        ps = con.prepareStatement(
                " select UserMaster1.usermasterid,UserMaster1.FName,UserMaster1.LName,UserMaster1.Gender,UserMaster1.address,UserLogin.Email,UserLogin.Password,UserMaster1.MobileNo,country.countryname,s.state,UserMaster1.hobby "
                        + " from UserMaster1 " 
                        + " INNER JOIN country  ON UserMaster1.country=country.countryid "
                        + " INNER JOIN state as s ON UserMaster1.state=s.stateid "
                        + " INNER join hobbyMaster ON UserMaster1.hobby=hobbyMaster.HobbyId "
                        + " INNER join UserLogin ON UserMaster1.usermasterid=UserLogin.usermasterid "
                        + " where UserMaster1.usermasterid=? ");
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            user = new User();
            user.setId(rs.getInt("usermasterid"));
            user.setFname(rs.getString("FName"));
            user.setLname(rs.getString("LName"));
            user.setGender(rs.getString("Gender"));
            user.setEmail(rs.getString("Email"));
            user.setPassword(rs.getString("Password"));
            user.setAddress(rs.getString("address"));
            user.setMobile_no(rs.getInt("MobileNo"));
            user.setCountry(rs.getString("countryid"));
            user.setState(rs.getString("stateid"));
            user.setHobby(rs.getInt("hobby"));
        }
    } finally {
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
    }
    return user;
}

it is returning the user from the method at the technology side i am using spring-mvc i suppose it is not of the concern

这是代码在执行时向我显示的错误。

java.sql.SQLException: Column 'countryid' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1073)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5212)
at in.co.xomic.dao.HomeDataAcessOperation.updateUsers(HomeDataAcessOperation.java:237)
at in.co.xomic.service.HomeService.updateUserById(HomeService.java:60)
at in.co.xomic.controller.HomeController.updateUser(HomeController.java:102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    原因是你通过rs.getString("countryid")获得了价值,但你没有在你的sql中选择它

    要解决它,您需要在选择中添加country.country.id

    select UserMaster1.usermasterid,UserMaster1.FName,UserMaster1.LName,
     UserMaster1.Gender,UserMaster1.address,UserLogin.Email,
    UserLogin.Password,UserMaster1.MobileNo,country.countryname,
    s.state,UserMaster1.hobby,country.countryid,s.stateid
    

    【讨论】:

    • 因为你已经在你的 sql 块中添加了国家名称,以便你可以在 jsp 页面中显示它
    • 你还会遇到同样的错误吗?还是 country.countryid 为空?
    • 干杯我刚刚做到了。谢谢你的帮助。我做了一些更改,我正在纠正。只是因为你不知道我的列名这已经发生了这不是你的原因完全是我和你为它付出了 100%。
    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2020-05-05
    • 2019-08-22
    相关资源
    最近更新 更多