3 Replies Latest reply: Jun 26, 2012 1:13 PM by dvohra21 RSS

    java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

    864822
      I have been able to create a connection to the database when I run as Java application, but when I try using the same connection information to run on the server, I get this exception. /any help with creating the conection for the server would be appreciated. Thanks.
      I am using Spring with the SpringSource sts2.8.1 - an MVC template
      jdk1.7.0-04,
      Oracle Database 11G Express Edition,
      Apache Tomcat 6.0.35

      type Exception report

      message

      description The server encountered an internal error () that prevented it from fulfilling this request.

      exception

      org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
           org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
           org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


      root cause
      java.lang.NullPointerException
           vicki.oracle.database.OracleDBHelper.listTitles(OracleDBHelper.java:52)
           vicki.oracle.movietime.HomeController.home(HomeController.java:39)
      java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
           at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
           at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
           at java.lang.Class.forName0(Native Method)
           at java.lang.Class.forName(Unknown Source)
           at vicki.oracle.database.OracleDBHelper.<init>(OracleDBHelper.java:30)
           at vicki.oracle.movietime.HomeController.home(HomeController.java:36)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      Here is the code that fails:
           Connection conn = null;
           private static final Logger Logger = LoggerFactory.getLogger(OracleDBHelper.class);
           
           public OracleDBHelper() {
                try{
                     Class.forName("oracle.jdbc.OracleDriver");
                     conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "movietime", "troyb");     
                }
                catch (Exception e) {
                     e.printStackTrace();
                }
           }

      public ArrayList<OracleTitle> listTitles() {
           
                Statement st;
                ArrayList<OracleTitle> result = new ArrayList<OracleTitle>();
                
                try {
                     st = conn.createStatement();
                     
                     ResultSet res = st.executeQuery("SELECT * FROM OracleTitle");
                     while (res.next()) {
                          OracleTitle t = new OracleTitle();
                          t.setId(res.getLong("id"));
                          t.setName(res.getString("name"));
                          t.setGenre(res.getString("genre"));
                          t.setPrice(res.getLong("price"));
                          t.setOnHand(res.getLong("onhand"));
                          result.add(t);
                     }
                     
                } catch (SQLException e) {
                     e.printStackTrace();
                }
                return result;

      Here is the code that works:
                          Class.forName("oracle.jdbc.OracleDriver");
                     Connection conn = DriverManager.getConnection(
                               "jdbc:oracle:thin:@//localhost:1521/XE", "movietime",
                               "troyb");

                     if (conn != null) {
                          System.out.println("We have connected to our database!");
                          Statement stmt = conn.createStatement();

                          ResultSet res = stmt.executeQuery("SELECT * FROM title");
                          while (res.next()) {
                               OracleTitle t = new OracleTitle();
                               Long tID = (res.getLong("title_id"));
                               String tName = (res.getNString("name"));
                               String tGenre = (res.getString("genre"));
                               Long tPrice = (res.getLong("price"));
                               Long tOnHand = (res.getLong("onhand"));
                               System.out.println(tID + " " + tName + " " + tPrice + " "
                                         + tOnHand);
                          }
                     }
                     conn.close();
                }

                catch (SQLException ex) {
                     System.out.println("SQLException: " + ex.getMessage());

                } catch (Exception ex) {
                     System.out.println("Exception: " + ex.getMessage());
                     ex.printStackTrace();
                }
           }
      }