Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class SysRun {
- public static void main(String[] args) {
- if (args.length != 6) {
- System.err.println("Usage: java SysRun server port user password database timeout");
- return;
- }
- Connection connection = null;
- int timeout = Integer.parseInt(args[5]);
- try {
- // Load the JDBC driver
- String driverName = "oracle.jdbc.driver.OracleDriver";
- Class.forName(driverName);
- // Create a connection to the database
- String serverName = args[0]; //"127.0.0.1";
- String portNumber = args[1]; //"1521";
- String sid = args[4]; //"mydatabase";
- String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
- String username = args[2]; //"username";
- String password = args[3]; //"password";
- connection = DriverManager.getConnection(url, username, password);
- } catch (ClassNotFoundException e) {
- // Could not find the database driver
- System.err.println("Couldn't find a driver for Oracle. Forgot to add the jar to the classpath?");
- return;
- } catch (SQLException e) {
- // Could not connect to the database
- System.err.println("Couldn't connect to the database. Wrong credentials?");
- return;
- }
- while (true) {
- try {
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM (SELECT sh_command, done, id FROM md_shell WHERE done IS NULL ORDER BY id) WHERE ROWNUM=1");
- String id = null;
- if (rs.next()) {
- String command = rs.getString(1);
- Process p;
- try {
- p = Runtime.getRuntime().exec(command);
- p.waitFor();
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("Error while executing \"" + command + "\"");
- } catch (InterruptedException e) {
- e.printStackTrace();
- System.err.println("The command \"" + command + "\" has been interrupted.");
- }
- id = rs.getString(3);
- }
- if (null != id) {
- stmt.execute("UPDATE md_shell SET done=1 WHERE id=" + id);
- connection.commit();
- }
- else {
- try {
- Thread.sleep(timeout * 1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- // but not of interest here
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- System.err.println("Error creating a connection statement or executing an SQL update/query.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement