gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:58:20 24 4
gpt4 key购买 nike

我被要求为我的项目通过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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com