DemoScript2.java

      import java.nio.charset.Charset;

      import java.time.LocalDateTime;
      import java.time.LocalTime;
      import java.time.format.DateTimeFormatter;

      import java.sql.Statement;
      import java.sql.ResultSet;
      import java.sql.PreparedStatement;
      import java.sql.Timestamp;

      public class DemoScript2 extends ScriptJava {

          Ref refLastDate;
          Ref refMsgText;
          Ref refMsgError;
          Ref refMsgSend;

          int time;
          String text;

          final static String dbname = "db";
          Database db;

          boolean connected;

          @Override
          public boolean load() {
              time = getArg("time", 800);
              refLastDate = createRef("lastdate");
              refMsgText  = createRef("msg", "text");
              refMsgError = createRef("msg", "error");
              refMsgSend  = createRef("msg", "send");

              return true;
          }

          @Override
          public boolean prepare() {
              db = getDatabase(dbname);
              if (db == null) {
                  printError("Database module is not found: " + dbname);
                  return false;
              }
              connected = false;
              return true;
          }

          @Override
          public void execute() {
              if (db == null || !isValid())
                  return;

              if (!connected && db.isConnected()) {
                  connected = true;
                  init();
              }

              if (connected && !db.isConnected()) {
                  connected = false;
              }

              if (connected)
                  update();
          }

          /// init ///
          private void init() {

              // some code

          }

          /// update ///
          private void update() {
              try (Statement st = db.getConnection().createStatement()) {

                    // some code

                  String sql = "select * from users";
                  try (ResultSet rs = st.executeQuery(sql)) {

                    // some code
                  }

                    // some code

              } catch (Exception e) {
                  printError(e, "Request error");
              }

          }
      }