I have been writing an application in Java which checks an SQL database for some information, then acts differently upon this depending on what values are read, I am trying to get the software in an endless loop, therefore will act every-time new information is added to the database. The software is using to print ZPL bar code labels off when they are added to the SQL database. I am wanting the program to be in a constant loop which checks if a new ProcessDate has been added, my code is as follows. Any help appriciated;
package com.company;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SqlCon
{
public static void main(String[] args)
{
// Connects too SQL database
String connectionString = "jdbc:sqlserver://acstdevsql01:1433;database=brad;user=USER;password=PW;";
// Selects data from database
String SQL = "SELECT TOP 2 [PK_PrintQueueID],[FK_PrinterID],[FK_BarcodeTypeID],[Barcode],[Quantity],[QueueDate],[ProcessedDate] FROM [Brad].[dbo].[PrintQueue] -- WHERE ProcessedDate IS NULL";
// Declare variable connection.
Connection connection = null;
// Set date format
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Get current date time with Date()
Date date = new Date();
try
{
connection = DriverManager.getConnection(connectionString);
Statement stmt = connection.createStatement();
Statement stmt2 = null;
ResultSet rs = stmt.executeQuery(SQL);
while (rs.next())
{
String FK_BarcodeTypeID = rs.getString("FK_BarcodeTypeID");
// Get barcode value to split form SQL result set
String barcode = rs.getString("Barcode");
String[] parts = barcode.split("-");
// First half of barcode
String part1 = parts[0];
// Set the processed date on any item which does not already have one set, The systems current time/date is used
String SQL2 = "UPDATE PrintQueue SET ProcessedDate = '"+dateFormat.format(date)+"' WHERE PK_PrintQueueID = '"+rs.getString("PK_PrintQueueID");
// Actions for serialized barcode (FK_BarcodeTypeID = 1)
if (FK_BarcodeTypeID.equals("1"))
{
// ^BC = Type 128 barcode
String zpl = "^XA^BY3,3,140^FT60,200^BCN,Y,N,N^FD>:"+rs.getString("Barcode")+"^FS^FT200,250^A0N,42,40^FH^FD"+part1+"^FS^XZ";
printlabel(zpl);
System.out.println(rs.getString("Barcode"));
}
// Actions for unserialized barcode (FK_BarcodeTypeID = 2)
else
{
// ^B3 = Type 30 barcode
String zpl = "CT~~CD,~CC^~CT~ ^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR4,4~SD15^JUS^LRN^CI0^XZ^XA^MMT^PW674^LL0376 ^LS0 ^BY2,3,151^FT84,249^BCN,,Y,N^FD>:P-GEN-SEA-BOX2ULTYPE^FS ^PQ1,0,1,Y^XZ";
printlabel(zpl);
System.out.println(rs.getString("Barcode"));
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void printlabel(String zpl)
{
try
{
Socket clientSocket;
clientSocket = new Socket("IP", PORT);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(zpl);
clientSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}