2012-11-07 77 views
1

我被要求通过java为我的项目对数据库表执行循环轮询。我是使用eclipse的Java中的一个begginer,迄今为止我只成功地将Java连接到oracle并将其转换为pojo对象。可以请您引导我...如何继续......? 她是我的代码在Java中针对数据库表执行循环轮询时间

public static void main(String[] args) throws SQLException { 

    DummyClass dc = new DummyClass(); 

    Connection conn = null; 
    Statement st = null; 
    ResultSet rs = null; 

    try { 
     System.out.println("JDBC connection"); 
     DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 
     conn = DriverManager.getConnection("jdbc:oracle:thin:@VM-SALES-MB:1521:SALESDB1", "bdeuser", "edb"); 
      st = conn.createStatement(); 
      rs = st.executeQuery("select * from msg_new_to_bde"); 
     conn = DriverManager.getConnection(
       "jdbc:oracle:thin:@VM-SALES-MB:1521:SALESDB1", "bdeuser", 
       "edb"); 
     st = conn.createStatement(); 
     rs = st.executeQuery("select * from msg_new_to_bde");*/ 

     Collection<KpiMessage> pojoCol = new ArrayList<KpiMessage>(); 
    while (rs.next()) {    
     KpiMessage filedClass = dc.convertRecordsetToPojo(rs); 
      pojoCol.add(filedClass); 

     } 
     for (KpiMessage pojoClass : pojoCol) { 
     System.out.print(pojoClass.getSequence()); 
     System.out.print(pojoClass.getTableName()); 
     System.out.print(pojoClass.getEntryTime()); 
     System.out.print(pojoClass.getProcessingTime()); 
     System.out.println(pojoClass.getStatus()); 
     } 
     System.out.print(pojoCol.size()); 

     System.out.println(pojoCol.size()); 
     System.out.println("close connection"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      rs.close(); 
      st.close(); 
      conn.close(); 

     } catch (Exception se) { 
      se.printStackTrace(); 
     } 

    } 

} 


/** 
* Converts a provided recordeset to a {@link KpiMessage}. 
* 
* The following attributs are copied from recordset to pojo: 
* 
* <ul> 
* <li>SEQ</li> 
* <li>TABLENAME</li> 
* <li>ENTRYTIME</li> 
* <li>STATUS</li> 
* </ul> 
* 
* @param rs the recordset to convert 
* @return the converted pojo class object 
* @throws SQLException if an sql error occurrs during processing of recordset 
*/ 
private KpiMessage convertRecordsetToPojo(ResultSet rs) throws SQLException { 

    KpiMessage msg = new KpiMessage(); 

    int sequence = rs.getInt("SEQ"); 
    msg.setSequence(sequence); 
    String tablename = rs.getString("TABLENAME"); 
    msg.setTableName(tablename); 
    Timestamp entrytime = rs.getTimestamp("ENTRYTIME"); 
    Date entryTime = new Date(entrytime.getTime()); 
    msg.setEntryTime(entryTime); 
    Timestamp processingtime=rs.getTimestamp("PROCESSINGTIME"); 
    if(processingtime!=null){ 
     Date processingTime = new Date(processingtime.getTime()); 
     msg.setProcessingTime(processingTime); 
    } 

    int status = rs.getInt("STATUS"); 
    msg.setStatus(status); 
    return msg; 

} 

}

+1

欢迎来到Stack Overflow!我们鼓励你[研究你的问题](http://stackoverflow.com/questions/how-to-ask)。如果你已经[尝试了某些东西](http://whathaveyoutried.com/),请将其添加到问题中 - 如果没有,请先研究并尝试您的问题,然后再回来。 – 2012-11-07 13:56:48

+1

谢谢你...我在这里新...所以现在就更新... – Babu

+1

@Tichodroma我必须做循环轮询这个代码使它每十秒钟读一次列.... – Babu

回答

1

重命名main()方法来MAIN2并添加这一个:

public static void main(String[] args) { 
    while(true) { 
     main2(args); 
     Thread.sleep(10*60*1000); // 10 minutes 
    } 
} 

这不是漂亮的代码,但工程。