java - 在 Java 中根据时间对数据库表执行循环轮询

标签 java database

我被要求为我的项目通过java对数据库表进行循环轮询。我是使用eclipse的Java初学者,到目前为止我只成功地将Java连接到oracle并将其转换为pojo对象。你能指导我...如何继续...? 她是我的代码

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;

}

}

最佳答案

将 main() 方法重命名为 main2 并添加此方法:

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

这不是很好的代码,但可以工作。

关于java - 在 Java 中根据时间对数据库表执行循环轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13271149/

相关文章:

mysql - 无法在mysql上创建触发器

java - 在游戏框架中编写功能测试的正确方法

java - 从依赖于其他项目的 Maven 项目构建 jar

java - 使用 luaj 将参数传递给 lua 函数

database - Java EE getResultList() 只返回对象而不是相关的类类型对象

mysql - 需要帮助选择不同的条目

java - 如果我想使用 Google App Engine 从服务器代码写入 Firebase,是否需要启用计费?

java - 为什么我必须添加父 pom 的依赖项?

SQL 概念 : how many ways to "add" column and row to a result set?

c# - 数据库未显示在 Visual Studio 中