2015-04-28 63 views
0

當我從內部連接的兩個表(MDA_AUDIT和MESSAGE)的結果中選擇列時,出現一些錯誤。但是,相同的查詢在SQL Developer界面中完美無瑕。通過內部連接兩個表通過java進行SQL查詢

這是我在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)); 
      } 
     } 
    } 
} 
+0

從結果集 –

+3

中提取數據時可能會出現問題,請使用PreparedStatement's。你的sql對注入攻擊很有用。 –

+1

你可以分享代碼嗎?你如何閱讀? – erencan

回答

0

查詢看起來很好;該問題必須出現在您用來訪問結果的代碼中,您必須要求提供不存在的列。

+0

相同的查詢工作正常時複製粘貼在SQL開發接口..所以我不認爲有一個不存在的列。 –

+0

正如我所說,查詢看起來不錯,因此應該在SQL Developer或任何地方工作;我擔心這個問題會出現在你的Java代碼中的任何地方(你還沒有顯示出來),你試圖從ResultSet中得到Int,因爲看起來你試圖恢復一個不屬於查詢的名字 –

+0

根據你的指示,問題在於ResultSet。查詢運行正常:resultSet = getResultSet(formattedQuery); ,並沒有有問題。當它進入功能: getCsvLogs(最終結果集的resultSet)拋出IOException異常 我能夠找出問題領域的一個: 最後絃樂的requestId = resultSet.getString(「REQUEST_ID」 ); 爲了解決問題,我按照查詢添加了表的別名,但仍然有相同的例外: final String requestId = resultSet.getString(「MDA_AUDIT.REQUEST_ID」); 你可以幫助在聯接的情況下應該檢索columnName的方式嗎? –