title: Running a JDBC oracle java programm from cmd line
body: <p>I have the following problem with running JDBC oracle from command line. When I run the following code in IntelliJ, it works like a charm, but when I try to run it via commandline with</p>
  
  <pre><code>java UserInterface filldb
  </code></pre>
  
  <p>I get the follwing error:</p>
  
  <blockquote>
    <p>An error occured: oracle.jdbc.driver.OracleDriver</p>
  </blockquote>
  
  <p>How do I run this program from command line?</p>
  
  <pre><code>import java.sql.*;
  import java.util.List;
  
  import oracle.jdbc.driver.*;
  
  import javax.security.auth.callback.ConfirmationCallback;
  
  
  final public class DatabaseDAO implements DAO {
  
      private String database;
      private String user;
      private String pass;
  
      // establish connection to database
      private Connection con;
      private Statement stmt;
  
      DatabaseDAO() throws ClassNotFoundException, SQLException{
          // load drivers and save user data
          Class.forName("oracle.jdbc.driver.OracleDriver");
          database = "jdbc:oracle:thin:@oracle-lab.cs.univie.ac.at:1521:lab";
          user = "*****";
          pass = "*****";
      }
  
      public void openConnection(String user, String database, String pass) throws SQLException{
              con = DriverManager.getConnection(database, user, pass);
              stmt = con.createStatement();
      }
  
      public void openConnection() throws SQLException{
          openConnection(user, database, pass);
      }
  
      public void closeConnection() { //local exception handling so method can be called in finally block, seems safer
          try {
              stmt.close();
              con.close();
          }
          catch (Exception e) {
              System.err.println("Fehler beim Verbindungsabbau in DatabaseDAO.closeConnection(): " + e.getMessage());
              System.exit(1);
          }
  
      }
  
      private void printTableSize(String tablename) throws SQLException {
          ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tablename);
          if (rs.next()) {
              int count = rs.getInt(1);
              System.out.println("Number of datasets in table " + tablename + ": "+ count);
          }
          rs.close();
      }
  
      private boolean insertSQL (String values, String tablename) throws SQLException {
          String insertSql = "INSERT INTO " + tablename + " VALUES " + values;
          stmt.executeUpdate(insertSql);
          return true;
      }
  
      public void flushTable(String tablename) throws SQLException{
          stmt.executeQuery("DELETE FROM " + tablename);
      }
  
      public void flushDatabase() throws  SQLException {
          for ( Relation r : Relation.values())
              flushTable(r.toString());
      }
  /*
      public ArrayList&lt;String&gt; getDatabaseEntry(int type, List&lt;String&gt; key) throws ClassNotFoundException, SQLException, IllegalArgumentException {
  
      }
  */
  /*
      public boolean deleteDatabaseEntry(List&lt;String&gt; key) throws ClassNotFoundException, SQLException, IllegalArgumentException{}
   */
      public boolean saveDatabaseEntry(Relation type, DatabaseObject entry){
          boolean success = false;
          try{
              switch (type) {
                  case Firma: {
                      Firma e = (Firma)entry;
                      String values = "(" + e.getSteuerbuchummer() + "," + e.getName() + "," + e.getFirmenbuchnummer() + ")";
                      printTableSize(Relation.Firma.toString());
                      success = insertSQL(values, Relation.Firma.toString());
                  } break;
                  case Template: {
                      Template e = (Template) entry;
                      String values = "(" + e.getUrl() + "," + e.getSprache() + "," + e.getIp() + ")";
                      printTableSize(Relation.Template.toString());
                      success = insertSQL(values, Relation.Template.toString());
                  } break;
                  case verwendet: {
                      verwendet e = (verwendet) entry;
                      String values = "(" + e.getSteuerbuchummer() + "," + e.getUrl() + ")";
                      printTableSize(Relation.verwendet.toString());
                      success = insertSQL(values, Relation.verwendet.toString());
                  } break;
                  case Unterseite: {
                      Unterseite e = (Unterseite) entry;
                      String values = "(" + e.getUrl() + "," + e.getPfad() + ")";
                      printTableSize(Relation.Unterseite.toString());
                      success = insertSQL(values, Relation.Unterseite.toString());
                  } break;
                  case Verkaufsseite: {
                      Verkaufsseite e = (Verkaufsseite) entry;
                      String values = "(" + e.getUrl() + "," + e.getPfad() + ", " + e.getTitel() + ")";
                      printTableSize(Relation.Verkaufsseite.toString());
                      success = insertSQL(values, Relation.Verkaufsseite.toString());
                  } break;
                  case UserAccount: {
                      UserAccount e = (UserAccount) entry;
                      String values = "(" + e.getEmail() + "," + e.getUserID() + ", " + e.getBankdaten() + ", " + e.getAdresse() + ")";
                      printTableSize(Relation.UserAccount.toString());
                      success = insertSQL(values, Relation.UserAccount.toString());
                  } break;
                  case befreundet: {
                      befreundet e = (befreundet) entry;
                      String values = "(" + e.getUserID1() + "," + e.getUserID2() + ")";
                      printTableSize(Relation.befreundet.toString());
                      success = insertSQL(values, Relation.befreundet.toString());
                  } break;
                  case Produkt: {
                      Produkt e = (Produkt) entry;
                      String values = "(" + e.getProduktID() + "," + e.getPreis() + "," + e.getProduktname() + ")";
                      printTableSize(Relation.Produkt.toString());
                      success = insertSQL(values, Relation.Produkt.toString());
                  } break;
                  case kauftEin: {
                      kauftEin e = (kauftEin) entry;
                      String values = "(" + e.getUrl() + "," + e.getUserID() + "," + e.getPfad() + "," + e.getProduktID() + "," + e.getAnzahl() + ")";
                      printTableSize(Relation.kauftEin.toString());
                      success = insertSQL(values, Relation.kauftEin.toString());
                  } break;
                  default:
                      throw new IllegalArgumentException("Parameter ungueltig in DatabaseDAO.saveDatabaseEntry(Relation type, DatabaseObject entry).");
              }
          }
          catch (Exception e)
          {
              System.err.println("Fehler beim Einfuegen des Datensatzes in DatabaseDAO.saveDatabaseEntry(Relation type, DatabaseObject entry): " + e.getMessage());
              success = false;
          }
          return success;
      }
  
      public Connection getCon() {
          return con;
      }
  
      public Statement getStmt() {
          return stmt;
      }
  }
  </code></pre>
  
  <p>Here is the main class:</p>
  
  <pre><code>import java.sql.SQLException;
  
  public class UserInterface {
      private enum Command {
          filldb, flushdb
      }
  
      private static final Command checkInput(String s){
          Command[] command = Command.values();
          for(int i = 0; i &lt; command.length; i++) if (command[i].toString().equals(s)) return command[i];
          throw new IllegalArgumentException("Commandline Parameter nicht erkannt. Kommandos: filldb, flushdb");
      }
  
      public static void main(String args[]) {
          DatabaseDAO db = null;
          try {
              Command command = checkInput(args[0]);
              switch(command){
                  case filldb: {
                      System.out.println("Initializing DB.");
  
                      db = new DatabaseDAO();
                      db.openConnection();
  
                      System.out.println("Done.\nGenerating data and filling DB (this may take a few minutes).");
                      TestDataGenerator tdg =  new TestDataGenerator(db);
                      tdg.testData(Relation.Firma, 2000);
                      tdg.testData(Relation.Template, 2000);
                      tdg.testData(Relation.verwendet, 500);
                      tdg.testData(Relation.Unterseite, 1000);
                      tdg.testData(Relation.Verkaufsseite, 1000);
                      tdg.testData(Relation.UserAccount, 3000);
                      tdg.testData(Relation.Produkt, 4000);
                      tdg.testData(Relation.befreundet, 500);
                      tdg.testData(Relation.kauftEin, 1000);
  
                      System.out.println("Done. DB is now filled with several thousand entries.");
                  } break;
                  case flushdb: {
                      System.out.println("Flushing DB");
                      db.flushDatabase();
                  }
              }
          }
          catch (SQLException e) {
              System.err.println("Ein Fehler ist bei der Kommunikation mit der Datenbank aufgetreten: "+ e.getMessage());
              System.exit(1);
          }
          catch (ClassNotFoundException e) {
              System.out.println("Klasse nicht gefunden: " + e.getMessage());
              System.exit(1);
          }
          catch (Exception e) {
              System.err.println("Ein Fehler ist aufgetreten: "+ e.getMessage());
              System.exit(1);
          }
          finally{
              if (!db.equals(null)) db.closeConnection();
              System.exit(0);
          }
      }
  }
  </code></pre>