2012-11-08 99 views
1

我有一些問題,從4gl到java執行相同的功能。 對4gl代碼有一些簡單的解釋。 首先,AFPOPF1是表,IO_AFPOPF1是存儲AFPOPF1的內容的變量。 使用'FETCH SCUR_1 INTO IO_AFPOPF1。*'語句對WK_QUERY進行調用並將行結果存儲到IO_AFPOPF1。*。 運行'FETCH SCUR_1 INTO IO_AFPOPF1。*'後,遊標引用下一行。 排除第二個查詢語句在while循環中包含IO_AFPOPF1.POLYN1的情況。如何將jdbc resultSet一般映射到用戶定義的類?

DEFINE IO_AFPOPF1 RECORD LIKE AFPOPF1.* 
    . 
    . 
    . 
WK_QUERY = "SELECT * FROM AFPOPF1 WHERE PRTDAT='IO_FONLY.PRTDAT' 
     " AND((FCERNO[2] = 'C' AND LENGTH(FCERNO) = 9)" 
     " OR (FCERNO[3] = 'C' AND LENGTH(FCERNO) = 10))" 
     " AND SOLIN1 LIKE '", BS_SOLIN1,"'" 
     " ORDER BY POLYN1,FCERNO"  
    . 
    . 
    . 
PREPARE SSTM_1 FROM WK_QUERY 
DECLARE SCUR_1 SCROLL CURSOR 
FETCH SCUR_1 INTO IO_AFPOPF1.* 

WHILE STATUS = 0 
    SELECT DEPTWN INTO WK_DEPTWN FROM COMLIB:DEPTPF1 
    WHERE DEPTNO = IO_AFPOPF1.POLYN1 
    . 
    . 
    . 
    IF WK_DEPTWN != IO_FONLY.DEPTWN 
     FETCH SCUR_1 INTO IO_AFPOPF1.* 
     CONTINUE WHILE 
    ELSE 
     LET SW_COUNT = SW_COUNT+1 
    END IF 
    . 
    . 
    . 
    FETCH SCUR_1 INTO IO_AFPOPF1.* 
END WHILE 

的java:

//the class mapping to Afpopf1 Table 
public class TableAfpopf1 { 
    private String fcerno;//var1 
    private String polyn1;//var2 
     . 
     . 
     . 
    private Date chkdat;//var58 

    public void setXXX 
     . 
     . 
     . 
} 

//database connection anf return resultSet 
public class DBConnection { 
    private Connection connection; 
    private Statement statement; 
    private PreparedStatement preS; 
    private ResultSet resultSet; 
    private boolean hasRow; 
    private boolean isReturn; 
    private String dbURL; 
    private ArrayList<ArrayList<Object>> allResultList; 
    private Object returnObj; 

    public DBConnection() { 
     this.connection = null; 
     this.statement = null; 
     this.preS = null; 
     this.resultSet = null; 
     this.hasRow = false; 
     this.isReturn = false; 
     this.dbURL = "";   
     this.allResultList = new ArrayList<ArrayList<Object>>(); 
    } 

public void connectDB(String query) { 
    try {    
     statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 

     if (query.contains("SELECT")) 
      resultSet = statement.executeQuery(query); 

     ResultSetMetaData metaData = resultSet.getMetaData(); 
     int numberOfColumns = metaData.getColumnCount(); 

     while (resultSet.next()) { 
      hasRow = true; 
      if (isReturn) {     
       if (numberOfColumns == 1) { 
        resultSet.last(); 
        int rowCount = resultSet.getRow(); 
        resultSet.first(); 

        if (rowCount == 1)       
         returnObj = resultSet.getObject(1);            
       } 
       else { 
        ArrayList<Object> arrayResultSet = new ArrayList<Object>(); 
        for (int i = 1; i <= numberOfColumns; i++) { 
         arrayResultSet.add(resultSet.getObject(i));       
        }      
        allResultList.add(arrayResultSet);      
       }     
      } 
      else      
       break;     
      }   
    } 
    catch (SQLException sqlEx) { 
     while(sqlEx != null) { 
      System.err.println("SQLException information"); 
      System.err.println("Error msg: " + sqlEx.getMessage()); 
      System.err.println("SQLSTATE: " + sqlEx.getSQLState()); 
      System.err.println("Error code: " + sqlEx.getErrorCode()); 
      sqlEx.printStackTrace(); 
      sqlEx = sqlEx.getNextException(); 
     } 
    }   
    finally { 
     try { 
      resultSet.close(); 
      statement.close();    
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     }  
    }      
} 

public void preQueryConnectDB(String query,String expression) { 
    try {    
     preS = connection.prepareStatement(query,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
     preS.setString(1, query); 
     resultSet = preS.executeQuery();    
     ResultSetMetaData metaData = resultSet.getMetaData(); 
     int numberOfColumns = metaData.getColumnCount();    

     while (resultSet.next()) {    
      hasRow = true; 
      if (isReturn) {     
       if (numberOfColumns == 1) { 
        resultSet.last(); 
        int rowCount = resultSet.getRow(); 
        resultSet.first(); 

        if (rowCount == 1)       
         returnObj = resultSet.getObject(1);            
       } 
       else { 
        ArrayList<Object> arrayResultSet = new ArrayList<Object>(); 
        for (int i = 1; i <= numberOfColumns; i++) { 
         arrayResultSet.add(resultSet.getObject(i));  
        }       
        allResultList.add(arrayResultSet);      
       }     
      } 
      else     
       break;     
     }    
    }catch (SQLException sqlEx) { 
     while(sqlEx != null) { 
      System.err.println("SQLException information"); 
      System.err.println("Error msg: " + sqlEx.getMessage()); 
      System.err.println("SQLSTATE: " + sqlEx.getSQLState()); 
      System.err.println("Error code: " + sqlEx.getErrorCode()); 
      sqlEx.printStackTrace(); 
      sqlEx = sqlEx.getNextException(); 
     } 
    }  
    finally { 
     try { 
      resultSet.close(); 
      preS.close();     
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    }      
} 

public void closeDBConnection() { 
    try { 
     connection.close();    
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void setIsReturnData(boolean isReturn) { this.isReturn = isReturn; } 

public void setConnectionURL(String query) { 
    String dbName = ""; 
    String newURL = ""; 
    int beginIndex = -1; //string from in index is positive 
    int endIndex = -1; //string : in index is positive 
    beginIndex = query.indexOf("FROM"); 
    endIndex = query.indexOf(":"); 
    dbName = query.substring(beginIndex+4, endIndex).trim(); 
    newURL = "jdbc:informix-sqli://hwae5500:shme5500/"+dbName+":INFORMIXSERVER=shme5500;"; 

    if (!newURL.equals(dbURL)) { 
     dbURL = newURL; 
     try { 
      if (connection != null) 
       connection.close(); 
       Class.forName("com.informix.jdbc.IfxDriver"); 
       connection = DriverManager.getConnection(dbURL); 
     }catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     catch(ClassNotFoundException drvEx) { 
      System.out.println("Driver fail"); 
      drvEx.printStackTrace(); 
     }  
    }   
} 

public void recoverDefultValue() { 
    this.statement = null; 
    this.preS = null; 
    this.resultSet = null; 
    this.hasRow = false; 
    this.isReturn = false;  
    this.returnObj = null;  
} 

public boolean getHasRow() { return hasRow; } 

public Object getReturnObj(){ return returnObj; } 

//get all resultSet in Object type 
public ArrayList<ArrayList<Object>> getAllResultList() { return allResultList; } 
} 

//the class receive query result and process data 
pubblic class DataProcessing { 
     . 
     . 
     . 
    public void generateInformation() { 
     . 
     . 
     . 
     sqlQuery = "SELECT * FROM AFFIL:AFPOPF1 WHERE PRTDAT="+"'"+dateStr+"'"+ 
      "AND FCERNO[2]='C' AND LENGTH(FCERNO)=9 "+ 
     "AND SOLIN1 LIKE '"+bs_SOLIN1+"' ORDER BY POLYN1,FCERNO"; 

     db.recoverDefultValue(); 
     db.setIsReturnData(true); 
     db.setConnectionURL(sqlQuery); 
     db.connectDB(sqlQuery); 
     if (db.getHasRow()) { 
      setDataList(db.getAllResultList(),1);//Object type to Afpopf1 type 
      sw_DATA = true;     
     } 
     . 
     . 
     . 
     if (codeStr.charAt(0) == '0') {    
      sqlQuery = "SELECT DEPTWN FROM COMLIB:DEPTPF1 WHERE DEPTNO=?"; 
      db.recoverDefultValue(); 
      db.setIsReturnData(true); 
      db.setConnectionURL(sqlQuery);   
      for (TableAfpopf1 tafp : Afpopf1List) {    
       db.preQueryConnectDB(sqlQuery,tafp.getPolyn1()); 
       wk_DEPTWN = setVarwk_DEPTWN((String)db.getReturnObj()); 
      }   
     } 
      . 
      . 
      .   
    } 

private void setDataList(ArrayList<ArrayList<Object>> allResultList,int choiceNum)                
{ 
    int inputindex = 0; 
    DateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");   
    if (choiceNum == 1) { 
     for (int i=0; i<allResultList.size(); i++) {     
      TableAfpopf1 afpItem = new TableAfpopf1(); 
      for (Object obj : allResultList.get(i)) { 
       switch (inputindex+1) { 
       case 1: 
        afpItem.setFcerno((String)obj); 
        break; 
       case 2: 
        afpItem.setPolyn1((String)obj); 
        break; 
        . 
        . 
        . 
       case 58:        
        try { 
         Date date = sdf.parse(obj.toString()); 
         afpItem.setChkdat(date);         
        } 
        catch (Exception e) { 
         System.out.println("error appear"); 
         e.printStackTrace(); 
        }           
        break;  
      }     
      inputindex++; 
     } 
     Afpopf1List.add(afpItem);    
     }   
    } 
    else if (choiceNum == 2) { 

    } 
}  

}


我認爲setDataList()和setDataList()不是好方法。 當我在java中實現一個程序時,setDataList()方法只映射到Afpopf1, ,我寫大小寫映射數據庫表的每個變量。它不是有效的代碼和愚蠢的。 然後,如果一個4gl程序有一個新的表變量,我必須爲該表寫另一個方法來接收數據。

我不認爲這是一個好主意,所以我想知道如何改進java代碼, 或告訴我在java中實現函數的正確方法。 我以前沒有碰過Java EE。

如果解決方案是關於Java EE的,我希望答案是詳細的或包含一些示例代碼。

回答

4

您可能會感到失望,但這是實際完成的方式,例如使用Spring JDBC模板:每個SQL查詢都將手動編碼映射到合適的bean對象中。

如果您希望自動完成,您應該考慮使用ORM(如Hibernate)而不是原始JDBC。

相關問題