我被要求通过 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;
}
}