1

我通过这段代码从我的 java 中的 COM 端口接收一些输入。

import java.io.*;
import java.util.*;

import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

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

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

输入是这样的:

447646

447647

447648

447649

我需要在名为“asset”的 MySql 表中输入这些值。包含两个字段ID,TIMESTAMP。ID 是上述串行输入,Timestamp 是输入发生的时间。

MySql 查询是什么样的?

st.executeUpdate("INSERT into asset VALUES(What to put here??);

一些帮助会很友善

更新代码:

好的,这是我的 SerialEvent 完整代码:

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 Id = new String(readBuffer);
            long timet = new getTime();
            st.executeUpdate("INSERT into asset(id,timet) VALUES("+id+","+timet+"");
            con.close();
        }
        catch(Exception ex){
            System.out.println(ex);
                    }
                    break;
        }
    }
4

1 回答 1

0
String id = new String(readBuffer);

String query = "INSERT into asset(ID,TIMESTAMP) VALUES(?,?)";
PreparedStatement pstm = con.prepareStatement(query);
pstm.setString(1, id);
pstm.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
pstm.executeUpdate();
于 2013-05-02T20:06:22.597 回答