• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

从cmd行运行JDBC oracle java程序

java 来源:Lorenz Kummer 4次浏览

从命令行运行JDBC oracle时出现以下问题。当我运行中的IntelliJ下面的代码,它就像一个魅力,但是当我尝试通过命令行与从cmd行运行JDBC oracle java程序

java UserInterface filldb 

我得到follwing错误运行:

An error occured: oracle.jdbc.driver.OracleDriver

如何运行这个程序从命令行?

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<String> getDatabaseEntry(int type, List<String> key) throws ClassNotFoundException, SQLException, IllegalArgumentException { 

    } 
*/ 
/* 
    public boolean deleteDatabaseEntry(List<String> 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; 
    } 
} 

这里是主类:

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


===========解决方案如下:

您需要指定包含Oracle驱动程序类路径,爪哇目前只盯着当前目录和它自己的运行时间类。

对于windows

java -cp .;C:\dummypath\ojdbc8.jar UserInterface filldb 

对于Linux

java -cp .:/dummypath/ojdbc8.jar UserInterface filldb 

.指定确保当前目录仍然在类路径中,C:\dummypath\/dummypath/需要与真正的位置被替换您的Oracle驱动程序jar。

旁注:我注意到你的课程UserInterface不在一个包中,这可能足够用于微不足道的程序,但你应该养成使用包的习惯。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)