找不到mySql列从servlet发生的错误

时间:2014-08-27 08:49:47

标签: java mysql servlets jdbc sqlexception

我查询数据库以从中获取所有值。查询有时会完美执行,但有时查询会失败,如下所示:

    select * from myTable  where objtype='EQUI' and scnrtype='C'
java.sql.SQLException: Column 'dataType' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1162)
    at com.mysql.jdbc.ResultSetImpl.getCharacterStream(ResultSetImpl.java:2056)
    at com.hastha.marerp.googlemaps.DBUtils.getScreenFields(DBUtils.java:672)
    at com.hastha.marerp.googlemaps.DBUtils.getScreenFieldsJSON(DBUtils.java:754)
    at com.hastha.marerp.googlemaps.DynamicMenuItemBuilder.doGet(DynamicMenuItemBuilder.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    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:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEondpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

dataType的数据类型为char(8)

我已经尝试了rs.getString()rs.getCharecterStream(),其中rs是从此查询中获取的ResultSet。我认为这对问题没有影响,因为错误发生在甚至达到此行之前。

This is a description of myTable

这是我的代码:

public ArrayList<ScenarioFieldInfo> getScreenFields(String objType, String scenarioType)
{
    System.out.println("Input args objType: "+objType+" and scenarioType: "+scenarioType);
    PreparedStatement pst = null;
    ResultSet rs = null;
    ArrayList<ScenarioFieldInfo> scenarioFieldList = new ArrayList<ScenarioFieldInfo>();
    try {
        String sql = "select * from myTable  where objtype='"+objType+"' and scnrtype='"+scenarioType+"'";
        System.out.println(sql);
        pst = (PreparedStatement) conn.prepareStatement(sql.toString());
        rs = pst.executeQuery();
        while(rs.next()){
            ScenarioFieldInfo fieldInfo = new ScenarioFieldInfo();
            fieldInfo.fieldName = rs.getString("fieldName");
            fieldInfo.fieldLabel = rs.getString("FieldLabel");
            //fieldInfo.sapType=rs.getString("dataType");
            BufferedReader reader=new BufferedReader(rs.getCharacterStream("dataType"));
            try {
                String sapType=reader.readLine();
                fieldInfo.sapType=sapType;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fieldInfo.parameter=rs.getString("PARAMETER");
            scenarioFieldList.add(fieldInfo);
        }
        System.out.println("There are "+scenarioFieldList.size()+" screen fields");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    finally
    {
        close(rs);
        close(pst);
    }
    return scenarioFieldList;
}

1 个答案:

答案 0 :(得分:0)

我发现代码崩溃了,因为我有getCharecterStream而不是getString导致我的情况出错。修改代码:

 rs.getString("dataType");

解决了问题