SQL查询通过java内部连接两个表

时间:2015-04-28 13:29:50

标签: java mysql oracle jdbc

当我从内部连接的两个表(MDA_AUDIT和MESSAGE)的结果中选择列时,我得到一些错误。但是,同样的查询在SQL Developer界面中完美运行。

这是我在java中运行的查询:

 return "select * from ( select a.*, rownum rnum from ("
        + " select MDA_AUDIT.REQUEST_ID, MDA_AUDIT.REQUEST_TYPE, MDA_AUDIT.REFERENCE_ID, MDA_AUDIT.MESSAGE, MESSAGE.STATUS, MESSAGE.ERROR_CODE,"
        + "MDA_AUDIT.MESSAGE_DATETIME, MESSAGE.CREATION_DATETIME, MESSAGE.LAST_MODIFIED_DATETIME, MESSAGE.RETURN_VALUE,MESSAGE.FAILURE_CODE,MESSAGE.REPLY_TO_QUEUE_NAME"
        + " from MESSAGE inner join MDA_AUDIT on MESSAGE.REQUEST_ID = MDA_AUDIT.REQUEST_ID where MDA_AUDIT.MESSAGE_DATETIME between '"+date1+"' and '"+date2+"') a where rownum <= %d )";

以下是错误:

[main] ERROR c.m.tools.report.OracleDataProvider - java.sql.SQLException: Invalid column name at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3665)
    at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2717)
    at oracle.jdbc.driver.ScrollableResultSet.findColumn(ScrollableResultSet.java:1866)
    at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:434)
    at com.morpho.tools.report.OracleDataProvider.getCsvLogs(OracleDataProvider.java:233)
    at com.morpho.tools.report.OracleDataProvider.getFirstPage(OracleDataProvider.java:129)
    at com.morpho.tools.report.LogExporter.main(LogExporter.java:122)

JDK版本= 1.8

以下是代码:

package com.morpho.tools.report;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class M<Final>
{
    private static Logger LOG = LoggerFactory.getLogger(OracleDataProvider.class);
    private SubscriptionInfo subscriptionInfo;
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private int paginationSize = 100;
    private int numOfRecords;

    private String getQuery(String date1, String date2)
    {   
        return "select * from ( select a.*, rownum rnum from ("
        + " select MDA_AUDIT.REQUEST_ID, MDA_AUDIT.REQUEST_TYPE, MDA_AUDIT.REFERENCE_ID, MDA_AUDIT.MESSAGE, MESSAGE.STATUS, MESSAGE.ERROR_CODE,"
        + "MDA_AUDIT.MESSAGE_DATETIME, MESSAGE.CREATION_DATETIME, MESSAGE.LAST_MODIFIED_DATETIME, MESSAGE.RETURN_VALUE,MESSAGE.FAILURE_CODE,MESSAGE.REPLY_TO_QUEUE_NAME"
        + " from MESSAGE inner join MDA_AUDIT on MESSAGE.REQUEST_ID = MDA_AUDIT.REQUEST_ID where MDA_AUDIT.MESSAGE_DATETIME between '"+date1+"' and '"+date2+"') a where rownum <= %d )";
    }

    private ResultSet getCandidatePin(String requestid)throws SQLException
    {
        ResultSet candidateSet = null;
        try
        {
            final String query1 = "SELECT FIRST_DUPLICATE_REFID FROM IDENTIFY_DETAIL where request_id='"+ requestid+"'";

            preparedStatement = connection.prepareStatement(query1,ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            candidateSet = preparedStatement.executeQuery();

        }catch (Exception e) {
        }
        return candidateSet;
    }

    public void open() throws ClassNotFoundException, SQLException
    {
        Class.forName(subscriptionInfo.getDriver());
        connection = DriverManager.getConnection(subscriptionInfo.getJdbcUrl(),"mda", "mda1234");       
    }
    public boolean hasMorePages()
    {
        boolean hasMorePages = false;
        numOfRecords = numOfRecords - paginationSize;
        LOG.info("Number of records pending : " + numOfRecords);
        if (numOfRecords > 0)
        {
            hasMorePages = true;
        }
        return hasMorePages;
    }

    private int getNumOfRecords() throws SQLException
    {
        ResultSet resultSet = null;
        int num = 0;
        try
        {
            final String query = "SELECT count(*) FROM message where MESSAGE_DATETIME between '26-JUN-13' and '27-JUN-13'";
            preparedStatement = connection.prepareStatement(query,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet = preparedStatement.executeQuery();
            resultSet.first();
            num = resultSet.getInt(1);
        }
        finally
        {
            if (null != resultSet)
            {
                resultSet.close();
            }
        }
        return num;
    }

    private ResultSet getResultSet(final String query) throws SQLException
    {
        preparedStatement = connection.prepareStatement(query,
                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        final ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    public List<CsvLog> getFirstPage(final int paginationSize, String date1, String date2)
            throws IOException
    {
        this.paginationSize = paginationSize;
        List<CsvLog> csvLogs = new ArrayList<CsvLog>();
        ResultSet resultSet = null;
        try
        {
            numOfRecords = getNumOfRecords();
            final String formattedQuery = String.format(getQuery(date1, date2),paginationSize, 1);
            resultSet = getResultSet(formattedQuery);
            csvLogs = getCsvLogs(resultSet);
        }
        catch (SQLException ex)
        {
            LOG.error(ExceptionUtils.getFullStackTrace(ex));
        }
        finally
        {
            if (null != resultSet)
            {
                try
                {
                    resultSet.close();
                }
                catch (SQLException e)
                {
                    LOG.warn(ExceptionUtils.getFullStackTrace(e));
                }
            }
        }
        return csvLogs;
    }

    public List<CsvLog> getNextPage(int startIndex, String date1, String date2) throws IOException
    {
        List<CsvLog> csvLogs = new ArrayList<CsvLog>();
        ResultSet resultSet = null;
        try
        {
            final String formattedQuery = String.format(getQuery(date1, date2), startIndex
                    + paginationSize, startIndex + 1);
            resultSet = getResultSet(formattedQuery);
            csvLogs = getCsvLogs(resultSet);
        }
        catch (SQLException ex)
        {
            LOG.error(ExceptionUtils.getFullStackTrace(ex));
        }
        finally
        {
            if (null != resultSet)
            {
                try
                {
                    resultSet.close();
                }
                catch (SQLException e)
                {
                    LOG.warn(ExceptionUtils.getFullStackTrace(e));
                }
            }
        }
        return csvLogs;
    }

    private List<CsvLog> getCsvLogs(final ResultSet resultSet)throws IOException
    {
        List<CsvLog> csvLogs = new ArrayList<CsvLog>();
        try
        {
            resultSet.first();
            do
            {
                final CsvLog csvLog = new CsvLog();

                final String requestId = resultSet.getString("REQUEST_ID");
                String requestType = resultSet.getString("REQUEST_TYPE");

                if (requestType.contentEquals("1"))
                {
                    requestType="INSERT";
                    csvLog.setMatchingResult("null");
                }
                else if (requestType.contentEquals("2"))
                {
                    requestType="IDENTIFY";
                    ResultSet GalleryPin= getCandidatePin(requestId);
                    //GalleryPin.next();
                    while(GalleryPin.next()){
                        String StringPin=GalleryPin.getString(1);
                        csvLog.setGalleryPin(StringPin);
                        csvLog.setMatchingResult("HIT");
                        csvLog.setNbCandidates(1);
                        csvLog.setSubtype("IDENTIFY_PIN");
                    }
                    if(!GalleryPin.first())
                    {
                        csvLog.setMatchingResult("NOHIT");
                        csvLog.setNbCandidates(0);
                        csvLog.setSubtype("IDENTIFY_PIN");
                    }

                }                           
                else if (requestType.contentEquals("3"))
                {
                    requestType="VERIFY";
                }
                else if (requestType.contentEquals("4"))
                {
                    requestType="DELETE";
                }

                final int result = resultSet.getInt("RETURN_VALUE");
                final Timestamp messageTimestamp = resultSet.getTimestamp("MESSAGE_DATETIME");
                final Timestamp beginTimestamp = resultSet.getTimestamp("CREATION_DATETIME");
                final Timestamp endTimestamp = resultSet.getTimestamp("LAST_MODIFIED_DATETIME");
                final String pin = resultSet.getString("REFERENCE_ID");
                final int errorCode = resultSet.getInt("FAILURE_CODE");
                final String messageClob = resultSet.getString("MESSAGE");

                csvLog.setMessage(messageClob);
                csvLog.setRequestId(requestId);
                csvLog.setRequestType(requestType);
                csvLog.setResult(result);
                csvLog.setMessageTimestamp(messageTimestamp);
                csvLog.setBeginTimestamp(beginTimestamp);
                csvLog.setEndTimestamp(endTimestamp);
                csvLog.setPin(pin);
                csvLog.setErrorCode(errorCode);
                csvLogs.add(csvLog);
            }
            while (resultSet.next());
        }
        catch (SQLException ex)
        {
            LOG.error(ExceptionUtils.getFullStackTrace(ex));
        }
        return csvLogs;
    }

    public SubscriptionInfo getSubscriptionInfo()
    {
        return subscriptionInfo;
    }

    public void setSubscriptionInfo(final SubscriptionInfo subscriptionInfo)
    {
        this.subscriptionInfo = subscriptionInfo;
    }

    public void closeQuitely()
    {
        if (null != preparedStatement)
        {
            try
            {
                preparedStatement.close();
            }
            catch (SQLException ex)
            {
                LOG.warn(ExceptionUtils.getFullStackTrace(ex));
            }
        }
        if (null != connection)
        {
            try
            {
                connection.close();
            }
            catch (SQLException ex)
            {
                LOG.warn(ExceptionUtils.getFullStackTrace(ex));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

查询看起来很好;问题必须在您使用的代码中才能访问结果,您必须要求不存在的列。