因此,我被赋予了一项任务,即使用TestNG框架从MS Excel文件作为Java数据库访问测试数据,我得到了这个例外。这是我保存Excel文件和其他Java类的方法。
下面给出了ExcelClass.java,其中我编写了包含所有函数的代码。
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<HashMap<String, Object>> read(String sql)
{
ResultSet resultSet = this.executeSql(sql,true);
List<HashMap<String, Object>> 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<HashMap<String, Object>> convertResultSetToList(ResultSet resultSet) {
// TODO Auto-generated method stub
int rowCtr = 0;
ResultSetMetaData rsmd = getResultSetMetaData(resultSet);
this.setColumnCount(resultSet);
List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
try {
while(resultSet.next())
{
HashMap<String, Object> row = new HashMap<String,Object>(columnCount);
for(int i=1;i<=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.");
}
}
}
下面是TestExcel类,我已经编写了测试来访问Excel文件。
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<HashMap<String, Object>> resultSet = excelClass.read(sql);
System.out.println("Row Count:"+excelClass.getRowCount());
System.out.println("Column Count:"+excelClass.getColumnCount());
for(int i=0;i<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();
}
}
}
以下是Excel文件中的内容。
这是我在代码中遇到的错误。
[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)
虽然我已经看到有一个UCANACCESS工具可以用作替代方法,因为JDK 8中出现了这个问题,但该工具不支持Excel,我不允许使用JDK 7.所以,任何人都可以请告诉我怎么办才能遇到这个问题?