-1

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

}

mrdarb
  • 101
  • 10
  • 1
    And what is your question? Do you expect us to read all your code, to then figure what maybe is missing from your requirements? Or is your problem really that you intend to connect to a database, but you don't know how to write down a loop that does something, sleeps a while, does its thing again, and so on? – GhostCat Jul 18 '16 at 12:02
  • Constant loop? You could make a scheduler that fires off a task every few seconds to run your queries – Sven Olderaan Jul 18 '16 at 12:08
  • http://stackoverflow.com/questions/12908412/print-hello-world-every-x-seconds – Sven Olderaan Jul 18 '16 at 12:09
  • @Jägermeister My problem is exactly that – mrdarb Jul 18 '16 at 12:09
  • 1
    @SvenOlderaan thank-you, appreciate the helpful feedback! – mrdarb Jul 18 '16 at 12:09
  • Side note: I have the vague feeling that your doing things in the wrong order. You should first learn about java in general, about its basic; how to do the really simple stuff. When you mastered that, and you got some experience and ideas about what makes sense; and what not; then you might start looking into advanced topics. Doing it the other way round ... opens up a **huge** room for many subtle or not so subtle bugs and problems. In other words: don't try to "build" applications that do "important" stuff ... by pulling together code examples from stack overflow. – GhostCat Jul 18 '16 at 12:12

1 Answers1

0

Not in that way, one eternal query. What would be fine, if the database could call java code on a trigger. Not possible for MS SQL Server AFAIK.

So you need a timer controlled "cron" job that queries if there is work to do. If you have other batch jobs to do, say at mid night, the a cron library in java might be fine. Search for cron, quartz, scheduler, timer.

(Message queues would be an alternative.)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138