MYSQL 数据库中插入或替换为空时的 Java 消息

标签 java mysql

我使用解析器从文本文件中读取数据并将其写入数据库。我想找到一种方法,当读数为空超过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实现类中,定义

  1. lastReadTime - 保存上次读取文件的时间。
  2. timeDiff - 保存从上次读取到当前时间的时间差。
  3. 如果间隔约为 30 分钟,则设置警告消息。
  4. 定义并调用刚刚出现的publishWarningMessage方法。发布可以保存到数据库中或通过短信/电子邮件/...等方式发送,具体取决于您的要求。
  5. 继续或等到下一次阅读。

关于MYSQL 数据库中插入或替换为空时的 Java 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808066/

相关文章:

java - 如何让 cxf-codegen-plugin 从 jakarta.xml.ws 生成 Webservices?

java - 将字符串拆分为多个字符序列

java - 为什么允许在接口(interface)中实现

java - 使用 CompletableFuture 抛出已检查的异常

MySQL 到 Oracle 语法错误(限制/偏移/更新)

php - 在数据库中设置 utf8 - 我可以从 php 代码中删除 SET NAMES utf8

java - 使用压缩生成损坏的 docx

mysql - 请解释这个mysql查询。

php - Between 子句返回 0 条记录

php - 从 mysql_connect 切换到 PDO : mysql_num_rows() expects parameter 1 to be resource