我被要求通过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;
}
}
答案 0 :(得分:1)
将main()方法重命名为main2并添加以下内容:
public static void main(String[] args) {
while(true) {
main2(args);
Thread.sleep(10*60*1000); // 10 minutes
}
}
这不是很好的代码,但有效。