MySql预期输出不会到来 [英] MySql expected output not coming

查看:46
本文介绍了MySql预期输出不会到来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个程序,它从COM端口读取串行输入,并将其放置在名为asset的MySql数据库中.

So I have this program which reads serial Input from COM port and places it in MySql database named asset.

代码:

import java.io.*;
import java.util.*;
import gnu.io.*;
import java.sql.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static Connection con=null;
static String url = "jdbc:mysql://localhost:3306/track";
static String uid="root";
static String pwd="root";

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
    portId = (CommPortIdentifier) portList.nextElement();
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
         if (portId.getName().equals("COM1")) {
    //                if (portId.getName().equals("/dev/term/a")) {
            SimpleRead reader = new SimpleRead();
                try
                {

                    Class.forName("com.mysql.jdbc.Driver");
                    con =     DriverManager.getConnection(url,uid,pwd);
                }
                catch(Exception s){
                    System.out.println(s);
                    }

    }
}
}
 }
 public SimpleRead() {
 try {
     serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
 } catch (PortInUseException e) {System.out.println(e);}
 try {
    inputStream = serialPort.getInputStream();
 } catch (IOException e) {System.out.println(e);}
 try {
    serialPort.addEventListener(this);
 } catch (TooManyListenersException e) {System.out.println(e);}
 serialPort.notifyOnDataAvailable(true);
 try {
    serialPort.setSerialPortParams(9600,
        SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
  } catch (UnsupportedCommOperationException e) {System.out.println(e);}
  readThread = new Thread(this);
  readThread.start();
  }

public void run() {
try {
    Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[20];

    try {
        while (inputStream.available() > 0) {
            int numBytes = inputStream.read(readBuffer);
        }
        System.out.print(new String(readBuffer));
    } catch (IOException e) {System.out.println(e);}

    Statement st=null;
    try{
        st=con.createStatement();

    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }

    try{
        String idd = new String(readBuffer);
        String query = "INSERT INTO asset (id) VALUES(?)";
        PreparedStatement pstm = con.prepareStatement(query);
        pstm.setString(1, idd);
        pstm.executeUpdate();
        pstm.close();
        con.close();

        //st.executeUpdate("insert into asset(id) VALUES('"+idd+"'");
        //con.close();
    }
    catch(Exception ex){
        System.out.println(ex);
                }
                break;
    }
}
}

因此,每当从COM端口接收数据时,上述代码就会在mysql表中创建一个条目.

So whenever the data is recieved from the COM port, an entry is created by the above code in mysql table.

预期的条目应类似于:

我得到的实际输出是:

实际上发生的是,一个来自com端口的输入正在表中创建两个条目,一个具有串行输入和时间戳,而另一个仅具有时间戳.

What actually happens is that One input from com port is creating two entries into the table one with the serial input and timestamp and one only with the timestamp.

我想要实现的是每个COM输入只有1个条目.像预期的图像一样.

What I want to achieve is that only 1 entry per COM input. Like the expected Image.

推荐答案

在将ID插入数据库之前检查ID-

Check ID before inserting it into the database -

    String idd = new String(readBuffer);
    if(!idd.equals("") || !idd.equals(null)){
       String query = "INSERT INTO asset (id) VALUES(?)";
       PreparedStatement pstm = con.prepareStatement(query);
       pstm.setString(1, idd);
       pstm.executeUpdate();
       pstm.close();
       con.close();
    }

这篇关于MySql预期输出不会到来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆