2016-11-22 61 views
0

我正在完成一個java中間級別項目,並且在運行我的測試類時遇到了運行時錯誤。我提供了我的DB_SETUP,DB_MANAGER和我的TEST_INSTRUMENT類的代碼。該錯誤與JDBC連接有關。任何幫助,將不勝感激。謝謝 !LoadJDBCDriver錯誤Netbeans

DB_MANAGER

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class DB_MANAGER { 

Logger logger = Logger.getLogger(DB_MANAGER.class.getName()); 

void loadJDBCDriver() { 

    try { 
     Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 
    } catch (ClassNotFoundException ex) { 
     logger.log(Level.SEVERE, null, ex); 
    } 
} 

void checkConnection() { 

    loadJDBCDriver(); 

    Connection dbConnection = null; 
    String strUrl = "jdbc:derby:billsdb;create=true"; 
    try { 
     dbConnection = DriverManager.getConnection(strUrl); 
    } catch (SQLException sqle) { 
     logger.log(Level.SEVERE, null, sqle.getStackTrace()); 
    } 

} 

Connection getConnection() { 

    loadJDBCDriver(); 

    Connection dbConnection = null; 
    String strUrl = "jdbc:derby:billsdb;create=true"; 
    try { 
     dbConnection = DriverManager.getConnection(strUrl); 
    } catch (SQLException sqle) { 
     logger.log(Level.SEVERE, null, sqle.getStackTrace()); 
    } 

    return dbConnection; 

} 
} 

DB_SETUP

import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.sql.Connection; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.sql.Statement; 
    import java.util.Scanner; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 

    public class DB_SETUP { 

Logger logger = Logger.getLogger(DB_MANAGER.class.getName()); 

void createTables() { 

    DB_MANAGER dmbgr = new DB_MANAGER(); 

    Connection con = dmbgr.getConnection(); 

    executeSqlScript(con, new File("sql\\createdb.txt")); 
} 

void insertSetupData() { 

    DB_MANAGER dmbgr = new DB_MANAGER(); 

    Connection con = dmbgr.getConnection(); 

    executeSqlScript(con, new File("sql\\insertdata.txt")); 
} 

public void showData() { 

    Statement stmt; 

    DB_MANAGER dmbgr = new DB_MANAGER(); 

    Connection con = dmbgr.getConnection(); 

    /** Prints all data from the USERDETAILS Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from USERDETAILS"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      String userName = results.getString(2); 
      String fName = results.getString(3); 
      String lName = results.getString(4); 
      logger.info(id + "\t\t" + userName + "\t\t" + fName + "\t\t" + lName); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the INSTRUMENTS Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from INSTRUMENTS"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      String type = results.getString(2); 
      String name = results.getString(3); 
      logger.info(id + "\t\t" + type + "\t\t" + name); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the VOTE Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from VOTE"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      String opendate = results.getString(2); 
      String closedate = results.getString(3); 
      logger.info(id + "\t\t" + opendate + "\t\t" + closedate); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the VOTEINST Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from VOTEINST"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      int voteid = results.getInt(2); 
      int instrid = results.getInt(3); 
      logger.info(id + "\t\t" + voteid + "\t\t" + instrid); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the BANKDETAILS Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from BANKDETAILS"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int accnum = results.getInt(1); 
      String name = results.getString(2); 
      int branch = results.getInt(3); 
      String collect = results.getString(4); 
      int userid = results.getInt(5); 
      logger.info(accnum + "\t\t" + name + "\t\t" + branch + "\t\t" + collect + "\t\t" + userid); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the USERVOTE Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from USERVOTE"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      int userid = results.getInt(2); 
      int voteinstid = results.getInt(3); 
      float percent = results.getFloat(4); 
      logger.info(id + "\t\t" + userid + "\t\t" + voteinstid + "\t\t" + percent); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the VOTERESULT Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from VOTERESULT"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      int voteinstid = results.getInt(2); 
      float avgpercent = results.getFloat(3); 

      logger.info(id + "\t\t" + voteinstid + "\t\t" + voteinstid + "\t\t" + avgpercent); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the MKTHISTORY Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from MKTHISTORY"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      float price = results.getFloat(2); 
      String update = results.getString(3); 
      int instrid = results.getInt(4); 

      logger.info(id + "\t\t" + price + "\t\t" + update + "\t\t" + instrid); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    /** Prints all data from the CORPEVENTS Table **/ 
    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from CORPEVENTS"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      String type = results.getString(2); 
      String date = results.getString(3); 
      String desc = results.getString(4); 
      int instrid = results.getInt(5); 

      logger.info(id + "\t\t" + type + "\t\t" + date + "\t\t" + desc + "\t\t" + instrid); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

    try { 
     stmt = con.createStatement(); 
     ResultSet results = stmt.executeQuery("select * from DIVIDEND"); 

     System.out.println("\n-------------------------------------------------"); 

     while (results.next()) { 
      int id = results.getInt(1); 
      double payment = results.getDouble(2); 
      String date = results.getString(3); 
      int instrid = results.getInt(4); 

      logger.info(id + "\t\t" + payment + "\t\t" + date + "\t\t" + instrid); 
     } 
     results.close(); 
     stmt.close(); 
    } catch (SQLException sqlExcept) { 
     logger.log(Level.SEVERE, null,sqlExcept); 
    } 

} 

public void executeSqlScript(Connection conn, File inputFile) { 

    // Delimiter 
    String delimiter = ";"; 

    // Create scanner 
    Scanner scanner; 
    try { 
     scanner = new Scanner(inputFile).useDelimiter(delimiter); 
    } catch (FileNotFoundException e1) { 
     logger.log(Level.SEVERE, null, e1); 
     return; 
    } 

    // Loop through the SQL file statements 
    Statement currentStatement = null; 
    while (scanner.hasNext()) { 

     // Get statement 
     String rawStatement = scanner.next(); 
     try { 
      // Execute statement 
      currentStatement = conn.createStatement(); 
      currentStatement.execute(rawStatement); 
     } catch (SQLException e) { 
      logger.log(Level.SEVERE, null, e); 
     } finally { 
      // Release resources 
      if (currentStatement != null) { 
       try { 
        currentStatement.close(); 
       } catch (SQLException e) { 
        logger.log(Level.SEVERE, null, e);; 
       } 
      } 
      currentStatement = null; 
     } 
    } 
} 
} 

TEST_INSTRUMENT

public class TEST_INSTRUMENT { 

public static void main(String[] args){ 

DB_MANAGER mngr = new DB_MANAGER(); 
    mngr.checkConnection(); 

    DB_SETUP sdb = new DB_SETUP(); 
    sdb.createTables(); 
    sdb.insertSetupData(); 



    DAO_INSTRUMENT iDAO = new DAO_INSTRUMENT(); 
    iDAO.getAllInstruments(); 
    /** This displays the newly edited tables, after each DAO is tested **/ 


    System.out.println(""); 
    System.out.println("All Instruments."); 
    for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){ 
     System.out.println(instru.getName());} 

    iDAO.insertInstru(new CLS_INSTRUMENT("BOND", "DAC BOND", "Blah", "Blah blah blah", "17.11.2014", 9, "sdsdas", 2, null, null, 0, 0.85)); 

    System.out.println(""); 
    System.out.println("Instrument Added."); 
    for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){ 
     System.out.println(instru.getName());} 

    iDAO.updateInstrument(new CLS_INSTRUMENT(1, "SHARE", "ABC LTD")); 
    System.out.println(""); 
    System.out.println("Instrument Editted."); 
    for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){ 
     System.out.println(instru.getName());} 

    iDAO.deleteInstrument(new CLS_INSTRUMENT(1, "SHARE", "ABC LTD")); 
    System.out.println(""); 
    System.out.println("Instrument Deleted."); 
    for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){ 
     System.out.println(instru.getName()); 
    } 
    } 
    } 

錯誤堆棧跟蹤

run: 
    Nov 22, 2016 2:19:39 PM is3312project.DB_MANAGER loadJDBCDriver 
    SEVERE: null 
    java.lang.ClassNotFoundException:  org.apache.derby.jdbc.EmbeddedDriver 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:264) 
    at is3312project.DB_MANAGER.loadJDBCDriver(DB_MANAGER.java:26) 
    at is3312project.DB_MANAGER.checkConnection(DB_MANAGER.java:34) 
    at is3312project.TEST_INSTRUMENT.main(TEST_INSTRUMENT.java:18) 
+1

發佈錯誤堆棧跟蹤。 –

+0

我更新了它。謝謝 – MLL

回答

0

根據您使用的Derby版本,您需要將derby.jar添加到CLASSPATH以解決此問題ClassNotFoundException

快速查看this可以解釋JDBC驅動程序是否與您用來連接的Derby數據庫一起提供的。

您還沒有提到任何有關您正在使用的項目的Derby數據庫版本的詳細信息,因爲這是您的JDBC驅動程序。

快速查看可用的德比下載 - 頁面here