java - 从 COM 到 MySql 的串行输入

标签 java mysql jdbc

我正在通过这段代码从我的 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;
        }
    }

最佳答案

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

关于java - 从 COM 到 MySql 的串行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346665/

相关文章:

python - django.db.utils.operationalError : (2059 ,"Authentication Plugin ' caching_sha2_password'")

java - 在 eclipse 中同时运行 32 位 jvm 和 64 位 jvm

java - Oracle JDBC 数据源将所有连接的自动提交属性设置为 false

java - 如何使用 Jersey 在POST请求正文中发送2个PoJos?

java - 确定 ServletContext.getResource() 返回的 URL 是否代表文件夹或文件

php - 从 PHP 中的 SELECT 框中获取值

java - 如何在文本区域中显示整列?

java - 如何在 Intellij IDEA 中正确配置 JBoss?我收到错误 :server instance not specified

java - 如何从主文件向数据库插入数据?

mysql - 用于社交网络应用程序的 Cassandra 而不是 MySQL