java - MySql expected output not coming -
so have program reads serial input com port , places in mysql database named asset.
code:
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 asset (id) values(?)"; preparedstatement pstm = con.preparestatement(query); pstm.setstring(1, idd); pstm.executeupdate(); pstm.close(); con.close(); //st.executeupdate("insert asset(id) values('"+idd+"'"); //con.close(); } catch(exception ex){ system.out.println(ex); } break; } } }
so whenever data recieved com port, entry created above code in mysql table.
the expected entry should :
what actual output :
what happens 1 input com port creating 2 entries table 1 serial input , timestamp , 1 timestamp.
what want achieve 1 entry per com input. expected image.
check id before inserting database -
string idd = new string(readbuffer); if(!idd.equals("") || !idd.equals(null)){ string query = "insert asset (id) values(?)"; preparedstatement pstm = con.preparestatement(query); pstm.setstring(1, idd); pstm.executeupdate(); pstm.close(); con.close(); }
Comments
Post a Comment