Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class SysRun {
  9.     public static void main(String[] args) {
  10.         if (args.length != 6) {
  11.             System.err.println("Usage: java SysRun server port user password database timeout");
  12.             return;
  13.         }
  14.         Connection connection = null;
  15.         int timeout = Integer.parseInt(args[5]);
  16.         try {
  17.             // Load the JDBC driver
  18.             String driverName = "oracle.jdbc.driver.OracleDriver";
  19.             Class.forName(driverName);
  20.  
  21.             // Create a connection to the database
  22.             String serverName = args[0]; //"127.0.0.1";
  23.             String portNumber = args[1]; //"1521";
  24.             String sid = args[4]; //"mydatabase";
  25.             String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
  26.             String username = args[2]; //"username";
  27.             String password = args[3]; //"password";
  28.             connection = DriverManager.getConnection(url, username, password);
  29.         } catch (ClassNotFoundException e) {
  30.             // Could not find the database driver
  31.             System.err.println("Couldn't find a driver for Oracle. Forgot to add the jar to the classpath?");
  32.             return;
  33.         } catch (SQLException e) {
  34.             // Could not connect to the database
  35.             System.err.println("Couldn't connect to the database. Wrong credentials?");
  36.             return;
  37.         }
  38.         while (true) {
  39.             try {
  40.                 Statement stmt = connection.createStatement();
  41.                 ResultSet rs = stmt.executeQuery("SELECT * FROM (SELECT sh_command, done, id FROM md_shell WHERE done IS NULL ORDER BY id) WHERE ROWNUM=1");
  42.                 String id = null;
  43.                 if (rs.next()) {
  44.                     String command = rs.getString(1);
  45.                     Process p;
  46.                     try {
  47.                         p = Runtime.getRuntime().exec(command);
  48.                         p.waitFor();
  49.                     } catch (IOException e) {
  50.                         e.printStackTrace();
  51.                         System.err.println("Error while executing \"" + command + "\"");
  52.                     } catch (InterruptedException e) {
  53.                         e.printStackTrace();
  54.                         System.err.println("The command \"" + command + "\" has been interrupted.");
  55.                     }
  56.                     id = rs.getString(3);
  57.                 }
  58.                 if (null != id) {
  59.                     stmt.execute("UPDATE md_shell SET done=1 WHERE id=" + id);
  60.                     connection.commit();
  61.                 }
  62.                 else {
  63.                     try {
  64.                         Thread.sleep(timeout * 1000);
  65.                     } catch (InterruptedException e) {
  66.                         // TODO Auto-generated catch block
  67.                         e.printStackTrace();
  68.                         // but not of interest here
  69.                     }
  70.                 }
  71.             } catch (SQLException e) {
  72.                 e.printStackTrace();
  73.                 System.err.println("Error creating a connection statement or executing an SQL update/query.");
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement