MySql预期输出不会到来

时间:2013-05-02 21:50:35

标签: java mysql jdbc

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

代码:

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表中的上述代码创建一个条目。

预期条目应为:

Expected Image

我获得了什么实际输出:

Real Image

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

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

1 个答案:

答案 0 :(得分:0)

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

    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();
    }