用于检查更改的常量循环

  • 本文关键字:常量 循环 用于 java
  • 更新时间 :
  • 英文 :


我一直在用 Java 编写一个应用程序,它检查 SQL 数据库以获取一些信息,然后根据读取的值对此采取不同的行动,我试图让软件进入无限循环,因此每次将新信息添加到数据库时都会采取行动。该软件用于在将ZPL条形码标签添加到SQL数据库时打印它们。我希望程序处于一个常量循环中,该循环检查是否已添加新的ProcessDate,我的代码如下。任何帮助;

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

}

不是那样,一个永恒的查询。如果数据库可以在触发器上调用 java 代码,那就好了。不适用于 MS SQL Server AFAIK。

因此,您需要一个计时器控制的"cron"作业来查询是否有工作要做。如果你有其他批处理作业要做,比如在午夜,java 中的 cron 库可能没问题。搜索 cron、石英、调度程序、计时器。

(消息队列是一种替代方法。

最新更新