title: How to fix the java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver for an Excel File?
body: <p>So, I was given a task to access the Test Data from MS Excel file as a Database in Java by using the TestNG framework and I am getting this exception. Here's how I saved the Excel file and other Java classes.</p>
  
  <p><a href="https://i.stack.imgur.com/M2WLL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M2WLL.png" alt="enter image description here"></a></p>
  
  <p>Given below was the ExcelClass.java in which I have written the code including all the functions.</p>
  
  <pre><code>package com.webdriver.tests;
  
  public class ExcelClass {
  
      private String excelFilePath;
      private int rowCount;
      private int columnCount;
      private Connection conn;
      private Statement stmt;
      private ResultSet resultSet;
  
      public ExcelClass(String filepath)
      {
          excelFilePath=filepath;
      }
  
      public List&lt;HashMap&lt;String, Object&gt;&gt; read(String sql)
      {
          ResultSet resultSet = this.executeSql(sql,true);
          List&lt;HashMap&lt;String, Object&gt;&gt; recordSet = convertResultSetToList(resultSet);
          closeConnection();
          return recordSet;
      }
  
      public void update(String sql)
      {
          this.executeSql(sql, false);
          closeConnection();
      }
  
      private ResultSet executeSql(String sql, boolean read)
      {
          // TODO Auto-generated method stub
          String absolutePath=getAbsoluteFilePath(excelFilePath);
          conn=getConnection(absolutePath,read);
          ResultSet resultSet = executeQueryOnExcel(conn,sql);
          return resultSet;
      }
      private String getAbsoluteFilePath(String excelFilePath) {
          // TODO Auto-generated method stub
          String filepath=getClass().getResource(excelFilePath).getFile();
          filepath=filepath.replaceFirst("/", "");
          return filepath;
      }
  
      private Connection getConnection(String excelFilePath, boolean read) {
          String username="";
          String password="";
          String connString;
          try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              if(read) {
                  connString=this.getReadConnectionString(excelFilePath);
              }else {
                  connString=this.getWriteConnectionString(excelFilePath);
              }
              conn=DriverManager.getConnection(connString, username, password);
          }catch(ClassNotFoundException e) {
              e.printStackTrace();
              //Assert.assertTrue(false,"Incorrect driver class");
          }catch(SQLException e) {
              Assert.assertTrue(false,"Connection with excel db failed");
          }
          return conn;
      }
      private String getReadConnectionString(String excelFilePath) {
          // TODO Auto-generated method stub
          String connString="jdbc:odbc:DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ="+excelFilePath+"";
          return connString;
      }
  
      private String getWriteConnectionString(String excelFilePath) {
          // TODO Auto-generated method stub
          String connString="jdbc:odbc:DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ="+excelFilePath+";ReadOnly=0;";
          return connString;
      }
  
      private ResultSet executeQueryOnExcel(Connection conn, String sql) {
          // TODO Auto-generated method stub
          try{
              stmt=conn.createStatement();
              stmt.execute(sql);
              resultSet=stmt.getResultSet();
          }catch(SQLException e) {
              Assert.assertTrue(false, "Failed to execute sql query.");
          }
          return resultSet;
      }
      private List&lt;HashMap&lt;String, Object&gt;&gt; convertResultSetToList(ResultSet resultSet) {
          // TODO Auto-generated method stub
          int rowCtr = 0;
          ResultSetMetaData rsmd = getResultSetMetaData(resultSet);
          this.setColumnCount(resultSet);
          List&lt;HashMap&lt;String, Object&gt;&gt; list = new ArrayList&lt;HashMap&lt;String,Object&gt;&gt;();
          try {
              while(resultSet.next()) 
              {
                  HashMap&lt;String, Object&gt; row = new HashMap&lt;String,Object&gt;(columnCount);
                  for(int i=1;i&lt;=columnCount;++i) 
                  {
                      row.put(rsmd.getColumnName(i), resultSet.getObject(i));
                  }
                  rowCtr = rowCtr + 1;
                  list.add(row);
              }
          }
          catch(SQLException e) 
          {
              Assert.assertTrue(false,"Failed to convert ResultSet to List.");
          }
          rowCount=rowCtr;
          return list;
      }
  
      public int getRowCount() {
          return rowCount;
      }
  
      public int getColumnCount() {
          return columnCount;
      }
  
      private ResultSetMetaData getResultSetMetaData(ResultSet rs) {
          // TODO Auto-generated method stub
          ResultSetMetaData rsmd = null;
          try {
              rsmd=rs.getMetaData();
          }catch(SQLException e) {
              e.printStackTrace();
          }
          return rsmd;
      }
  
      private void setColumnCount(ResultSet resultSet) {
          // TODO Auto-generated method stub
          ResultSetMetaData rsmd = getResultSetMetaData(resultSet);
          try {
              columnCount=rsmd.getColumnCount();
          }catch(SQLException e) {
              Assert.assertTrue(false, "Failed to retrieve column count.");
          }
      }
  
      private void closeConnection() {
          // TODO Auto-generated method stub
          try {
              stmt.close();
              conn.close();
          }catch(SQLException e) {
              Assert.assertTrue(false, "Failed to close Excel DB connection.");
          }
      }
  }
  </code></pre>
  
  <p>Given below was the TestExcel class where I have written the test to access the Excel file.</p>
  
  <pre><code>package com.webdriver.tests;
  
  public class TestExcel {
      @Test
      public void testExcelDBRead()
      {
          //System.out.println(System.getProperty("java.class.path"));
          ExcelClass excelClass = new ExcelClass("/com/webdriver/testdata/CurrencyConverter.xlsx");
          String sql = "Select * from [CurrencyConversion$]";
          List&lt;HashMap&lt;String, Object&gt;&gt; resultSet = excelClass.read(sql);
          System.out.println("Row Count:"+excelClass.getRowCount());
          System.out.println("Column Count:"+excelClass.getColumnCount());
          for(int i=0;i&lt;resultSet.size();i++)
          {
              System.out.println(resultSet.get(i).get("KEY")+"\t");
              System.out.println(resultSet.get(i).get("KEYNAME")+"\t");
              System.out.println(resultSet.get(i).get("AMOUNT")+"\t");
              System.out.println(resultSet.get(i).get("FROM")+"\t");
              System.out.println(resultSet.get(i).get("TO")+"\t");
              System.out.println();
          }
      }
  }
  </code></pre>
  
  <p>Given below is the content in the Excel file.</p>
  
  <p><a href="https://i.stack.imgur.com/f5eXc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f5eXc.png" alt="enter image description here"></a></p>
  
  <p>This is the error which I am getting in the code. </p>
  
  <pre><code>[RemoteTestNG] detected TestNG version 6.12.0
  java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at com.webdriver.tests.ExcelClass.getConnection(ExcelClass.java:64)
      at com.webdriver.tests.ExcelClass.executeSql(ExcelClass.java:48)
      at com.webdriver.tests.ExcelClass.read(ExcelClass.java:32)
      at com.webdriver.tests.TestExcel.testExcelDBRead(TestExcel.java:15)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
      at org.testng.TestRunner.privateRun(TestRunner.java:776)
      at org.testng.TestRunner.run(TestRunner.java:634)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
      at org.testng.SuiteRunner.run(SuiteRunner.java:334)
      at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
      at org.testng.TestNG.runSuites(TestNG.java:1161)
      at org.testng.TestNG.run(TestNG.java:1129)
      at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
  FAILED: testExcelDBRead
  java.lang.NullPointerException
      at com.webdriver.tests.ExcelClass.executeQueryOnExcel(ExcelClass.java:94)
      at com.webdriver.tests.ExcelClass.executeSql(ExcelClass.java:49)
      at com.webdriver.tests.ExcelClass.read(ExcelClass.java:32)
      at com.webdriver.tests.TestExcel.testExcelDBRead(TestExcel.java:15)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
      at org.testng.TestRunner.privateRun(TestRunner.java:776)
      at org.testng.TestRunner.run(TestRunner.java:634)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
      at org.testng.SuiteRunner.run(SuiteRunner.java:334)
      at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
      at org.testng.TestNG.runSuites(TestNG.java:1161)
      at org.testng.TestNG.run(TestNG.java:1129)
      at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
  </code></pre>
  
  <p>Although I have seen that there is a UCANACCESS tool to use as an alternative because this problem occurs in JDK 8 but that tool doesn't support Excel and I am not permitted to use the JDK 7. So, can anyone kindly suggest me what can I do to encounter this problem?</p>