我使用解析器从文本文件中读取数据并将其写入数据库。我想找到一种方法,当读数为空超过2次时,发送错误消息。
我该怎么做?
这可能吗?
我希望我已经明确表示怀疑。
如果有人不明白请我澄清。
感谢您的帮助。
解析器的代码是:
BufferedReader reader = new BufferedReader(new FileReader("C:/Users/RPR1BRG/Desktop/test.txt"));
String dados[] = new String[6];
String linha = reader.readLine();
while (linha != null) {
StringTokenizer st = new StringTokenizer(linha, ";\"");
dados[0] = st.nextToken();
dados[1] = st.nextToken();
dados[2] = st.nextToken();
dados[3] = st.nextToken();
dados[4] = st.nextToken();
dados[5] = st.nextToken();
DateFormat dateFormat = new SimpleDateFormat("d-M-yy");
PreparedStatement stmt = (PreparedStatement) conexao.prepareStatement("replace into registos" + " (date, hour, Id, Tem, Hum, pt) values (?,?,?,?,?,?)");
try {
stmt.setDate(1, new java.sql.Date(dateFormat.parse(dados[0]).getTime()));
stmt.setString(2, dados[1]);
stmt.setString(3, dados[2]);
stmt.setString(4, dados[3]);
stmt.setString(5, dados[4]);
stmt.setString(6, dados[5]);
} catch (java.text.ParseException ex) {
Exceptions.printStackTrace(ex);
}
stmt.executeUpdate();
最佳答案
Do you mean that, if with an interval of 30 minutes each, if a file is read twice in an hour, and if these successive readings found an empty file, then a warning action has to be fired. Is that right? – Ravinder
Yes that is exactly what happens. I dont know is how to create warning The function to read file is executed within a timer function... – user3320956
解决方案:
在Timer
实现类中,定义
lastReadTime
- 保存上次读取文件的时间。timeDiff
- 保存从上次读取到当前时间的时间差。- 如果间隔约为 30 分钟,则设置警告消息。
- 定义并调用刚刚出现的
publishWarningMessage
方法。发布可以保存到数据库中或通过短信/电子邮件/...等方式发送,具体取决于您的要求。 - 继续或等到下一次阅读。
关于MYSQL 数据库中插入或替换为空时的 Java 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808066/