'Java - Calling a secondary class from another class
I am not a java developer but I need to update some java code that reads RFID tags to then update our MySQL database. I have two working examples where I can read some tags in one file and I can update the database in another file but when combining them I am out of my league.
I added public static void JavaDatabaseUpdate to my working code for the RFID reader and as long as I don't call it, the code executes fine. The problem comes when I try to run the script... Maybe I am just approaching this all wrong:
public class ReadingTags {
static final String DEFAULT_READER_IP_ADDRESS = "192.168.1.5";
static final int PORT_NUMBER = 5000;
static final int maxResponseSize = 1000;
static RFIDReader reader = null;
public static boolean reading = false;
public static void main(String[] args){
try
{
String readerIPAddress = DEFAULT_READER_IP_ADDRESS;
int PortNumber = PORT_NUMBER;
System.out.println("Setting up ReaderComm instance on IP Address " + readerIPAddress);
reader = new RFIDReader(readerIPAddress, PortNumber);
ReadTest();
}
catch (Exception ex)
{
System.out.println("Main: " + ex.toString());
}
}
public static void ReadTest() throws Exception, InterruptedException
{
TagReadHandler tagReadHandler = new TagReadHandler();
ReadingTags.reader.SetTagReadCallback(tagReadHandler);
System.out.println("Starting Continuous Read ...");
ReadingTags.reading = true;
ReadingTags.reader.StartContinuousRead();
// Read for .050 seconds then issue stop command
Thread.sleep(50);
System.out.println("*** Stopping Continuous Read For 5 Seconds to wait on another tray. We would do an update here****");
ReadingTags.reader.StopContinuousRead();
Thread.sleep(5000);
//Now start a new loop
System.out.println("Start New Loop");
ReadTest();
//While loop to read queued up
//tag data and to wait for stop command response.
while(ReadingTags.reading)
{
Thread.sleep(10);
}
}
public static void JavaDatabaseUpdate(String[] args) {
Connection conn = null;
Statement stmt = null;
String rfid = "145131000000000000000000";
try {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (Exception e) {
System.out.println(e);
}
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/office", "root", "");
stmt = (Statement) conn.createStatement();
String query1 = "update productiontrays set status=5 " + "where rfid ="+rfid;
stmt.executeUpdate(query1);
System.out.println("Record has been updated in the table successfully");
} catch (SQLException excep) {
excep.printStackTrace();
} catch (Exception excep) {
excep.printStackTrace();
} finally {
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
And then the class where I would like to call the JavaDatabaseUpdate:
class TagReadHandler implements OnTagReadResponse{
public void handleTagRead(TagReadInfo readInfo)
{
if (readInfo.tagReadStatus == 0) // STATUS_OK
{
System.out.println(readInfo.EPC.replaceAll("[^a-zA-Z0-9]", ""));
//I'd like to call JavaDatabaseUpdate(); here with the EPC value passed along for the update
}
else if (readInfo.tagReadStatus == 1) // STATUS_DONE
{
ReadingTags.reading = false;
System.out.println("Received Stop Response");
}
}
}
I really need to pass that EPC value back to the class and update the database. How should I approach this situation? I'm stuck.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
