在这里,该方法读取具有唯一 ID 且序列号不断增加的数据库,因为我是 java 初学者,我能否知道如何实现这种重复轮询并每次检查新传入消息。
/**
* Method which defines polling of the database and also count the number of Queries
* @return pojo collection
* @throws Exception
*/
public List<KAMessage> fullPoll() throws Exception {
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 order by SEQ DESC");
List<KAMessage> pojoCol = new ArrayList<KAMessage>();
while (rs.next()) {
KAMessage filedClass = convertRecordsetToPojo(rs);
pojoCol.add(filedClass);
}
return pojoCol;
}
/**
* Converts a provided record-set to a {@link KAMessage}.
*
* The following attributes are copied from record-set to pojo:
*
* <ul>
* <li>SEQ</li>
* <li>TABLENAME</li>
* <li>ENTRYTIME</li>
* <li>STATUS</li>
* </ul>
*
* @param rs
* @return the converted pojo class object
* @throws SQLException
*
*/
private KAMessage convertRecordsetToPojo(ResultSet rs) throws SQLException {
KAMessage msg = new KAMessage();
int sequence = rs.getInt("SEQ");
msg.setSequence(sequence);
int action = rs.getInt("ACTION");
msg.setAction(action);
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);
}
String keyInfo1 = rs.getString("KEYINFO1");
msg.setKeyInfo1(keyInfo1);
String keyInfo2 = rs.getString("KEYINFO2");
msg.setKeyInfo2(keyInfo2);
return msg;
}
}
这是我尝试过的:
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
如何使用准备好的语句将参数传递到此代码的查询中。
public List<KAMessage> fullPoll() throws Exception {
PreparedStatement oldSeq = null;
PreparedStatement newSeq = null;
Statement st = dbConnection.createStatement();
System.out.println("Polling");
String query = "select * from msg_new_to_bde where ACTION = 804 and SEQ between oldSeq and newSeq order by SEQ DESC";// insert in table
pstmt = conn.prepareStatement(query);
}