java - serial input from COM into MySql -
i receiving input com port in java through code.
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; } }}
the input these:
447646
447647
447648
447649
i need enter these values in mysql table named 'asset '. containing 2 fields id,timestamp. id being above serial inputs , timestamp time @ input occured.
what mysql query ?
st.executeupdate("insert asset values(what put here??);
some kindful
the update code:
ok complete code 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 asset(id,timet) values("+id+","+timet+""); con.close(); } catch(exception ex){ system.out.println(ex); } break; } }
string id = new string(readbuffer); string query = "insert asset(id,timestamp) values(?,?)"; preparedstatement pstm = con.preparestatement(query); pstm.setstring(1, id); pstm.settimestamp(2, new timestamp(system.currenttimemillis())); pstm.executeupdate();
Comments
Post a Comment