Return an ms access column into an array

Daliseiy :

I'm trying to return a column of scores from a MS Access table using java. This is the tableenter image description here

I followed the guide from the java documentation.This is the result code

public class DatabaseConnector {
    public static void main(String[] args){
        Connection connection=null;
        Statement statement =null;
        ResultSet resultSet = null;

        //Loads JDBC DRIVER
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        }catch (ClassNotFoundException cnfex){
            System.out.println("There was a problem loading MS Access Driver");
            cnfex.printStackTrace();
        }

        //Loads Database
        try{
            String scoredb="C:/Users/User/"+"/Documents/Database2.accdb";
            String mydburl ="jdbc:ucanaccess://"+scoredb;
            connection = DriverManager.getConnection(mydburl);
            statement = connection.createStatement();


            resultSet = statement.executeQuery("SELECT CSC103,  CSC103 FROM Scores");

            while(resultSet.next()) {
                Array z = resultSet.getArray("CSC103");
                int[] CSC103 = (int[])z.getArray();
                for (int i=0;i<CSC103.length;i++){
                    System.out.println(i);
                }

            }

        } catch (SQLException sqlex){
            sqlex.printStackTrace();
        }

        finally {
            try{
                if (null != connection) {
                    resultSet.close();
                    statement.close();
                    connection.close();
                }
            }catch (SQLException sqlex){
                sqlex.printStackTrace();
            }
        }

I ended up getting the error-

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to [Ljava.lang.Object;
    at org.hsqldb.jdbc.JDBCResultSet.getArray(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getArray(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessResultSet.getArray(UcanaccessResultSet.java:184)
    at DatabaseConnector.main(DatabaseConnector.java:28)

I have taken a look at this error online and it's a bit confusing because the solutions don't apply to database. How do I fix the error and get my values from the column?

Gord Thompson :

ResultSet#getArray is used with databases that support special columns in which we can store an array of values for each row in the table. Access does not have an Array column type. The CSC103 column in your table contains a single ("scalar") value for each row, an integer in this case.

So you need to retrieve the individual values for each row and add them to a collection of some sort. The most straightforward way would be

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT CSC103 FROM Scores");
java.util.List<Integer> csc103List = new java.util.ArrayList<>();
while (rs.next()) {
    csc103List.add(rs.getInt("CSC103"));
}

after which you'll have the values in an ArrayList<Integer>. If you really need an array of type Integer (which is a slightly different thing) then you can convert the List into an array like so:

Integer[] csc103Array = csc103List.toArray(new Integer[0]);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327113&siteId=1
ms