MS Access通过Java代码返回空记录集

时间:2017-05-10 15:42:50

标签: java ms-access

我正在尝试检索简单查询的结果,以检查我的连接是否正常工作。连接成功,但只返回空记录集。有问题的行标有注释

以下是我的代码

             package ActualProject;
             import java.io.FileInputStream;
             import java.io.FileNotFoundException;
             import java.io.IOException;
             import java.sql.*;

             import net.ucanaccess.jdbc.*;
             import org.apache.poi.ss.usermodel.Row;
             import org.apache.poi.ss.usermodel.Sheet;
             import org.apache.poi.xssf.usermodel.XSSFWorkbook;

             public class MainClass {
             static String strURL = null;
             public MainClass() {
                        }

public static void main(String[] args) {

    String strUserDir = System.getProperty("user.dir");
    String strProjectPath = strUserDir.replaceAll("ActualProject", "");
    String strDataPath = strProjectPath + "Data\\Data.xlsx";
    String TestCaseDescription = null;
    String strTestCaseName = null;


    String TestCaseSheetPath = strProjectPath + "ActualProject\\TestCaseSheet\\TestCaseSheet.mdb";

    //Code to read TestCaseSheet
    try
    {
         Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");


        System.setProperty("DUCANACCESS_HOME","C:/Users/mindurka/
        Downloads/Selenium3.4/UCanAccess-4.0.2-bin/UCanAccess-4.0.2-bin");

         String connURL = "jdbc:ucanaccess://"+TestCaseSheetPath;
         Connection objAccessCon = DriverManager.getConnection(connURL);
         DriverManager.getConnection(connURL1);
         Statement objRS = objAccessCon.createStatement();

    //           String strEnvQuery = "Select * from Env where Use = 'No'";
         String strEnvQuery = "Select * from Env";
         objRS.executeQuery(strEnvQuery);
         ResultSet objEnvRS = objRS.getResultSet();


         while(objEnvRS.next())
         {
             strURL = objEnvRS.getString("URL");
             String strQuery = "Select * from TestCaseSheet where Execute = 'Yes'";
             objRS.execute(strQuery);
             ResultSet RS = objRS.getResultSet();
             if(RS != null)
             {
                 while (RS.next()) 
                {
                     TestCaseDescription = RS.getString("TestCaseDescription");
                     strTestCaseName = RS.getString("TestCaseName");
                     FileInputStream objFIS = new FileInputStream(strDataPath);
                     XSSFWorkbook objDataWB = new XSSFWorkbook(objFIS);
                     Sheet objActiveSheet = objDataWB.getSheet("RegDetails");

                     for(int intRowCounter = 0 ; intRowCounter < objActiveSheet.getLastRowNum(); intRowCounter++)
                     {
                         Row objRow = objActiveSheet.getRow(intRowCounter);
                         for(int intColumnCounter = 0; intColumnCounter < objRow.getLastCellNum(); intColumnCounter++)
                         {
                             System.out.println("Row=="+ intRowCounter + "Column =="+ intColumnCounter);
                         }
                     }

                     objActiveSheet= null;
                     //objRow = null;
                    objRS.close();
                    RS.close();
                    objAccessCon.close();

                }
             }
             else
             {
                 System.out.println("No testcases selected for execution");
             }




        }


    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{

    }


}

}

Java版本:8 Eclipse版本:Neon MS Access版本:2007

如果需要任何其他详细信息,请与我们联系。 编辑评论:添加文件MSAccessScreenshot

的屏幕截图

2 个答案:

答案 0 :(得分:0)

想出来。 createStatement无效,因此尝试使用PrepareStatement,如下所示。

try
    {
         Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
         System.setProperty("DUCANACCESS_HOME","C:/Users/mindurka/Downloads
         /Selenium3.4/UCanAccess-4.0.2-bin/UCanAccess-4.0.2-bin");

         String connURL = "jdbc:ucanaccess://"+TestCaseSheetPath;
         Connection objAccessCon = DriverManager.getConnection(connURL);

         String strEnvQuery = "Select * from Env where Use = 'Yes'";

         PreparedStatement pst = objAccessCon.prepareStatement(strEnvQuery);

         ResultSet objEnvRS = pst.executeQuery();


         //ResultSet objEnvRS = pst.getResultSet();

         Statement objRS = objAccessCon.createStatement();
         while(objEnvRS.next() == true) //shows true for objEnvRS.next() == true
         {strURL = objEnvRS.getString("URL");
         }

我有一个更新的问题。虽然监视变量'objEnvRS.getString(“URL”)'显示正确的值,但编程方式我的代码不会进入while循环。是否需要设置某种结果集/连接属性来循环结果集?

答案 1 :(得分:0)

得到了我后来的问题的答案。我试图用watcvh部分中的objEnvRS.next()浏览记录集。这导致指针在while循环中比所需记录集行向前移动一步。在执行/调试任何代码之前,将始终记得清除监视部分。 :)