0

Hi everybody I'm having an issue returning results for queries that aren't exactly simple but aren't very complex either. I am overhauling an inhouse system from c++ to java. Simple one table select statements run fine but joining more than 2 I get nothing returned. I know the query works because I dropped it into sqlserver and it executed correctly not to mention it runs daily in the old system. Can't attach image so below is my simple sqlConnect function. I'll try and break the query up so its readable.

The output I get is this.

run:
Connected to the server successfully.
2
1000
While ENDED
BUILD SUCCESSFUL (total time: 1 second)

Code:

    public Connection sqlConnect(){    
                    Connection sqlConn = null;
                    Statement stmt = null;
                    String SQL_SELECT = "SELECT SUM(((InventTrans.qty*(-1))*InventTable.netweight)/2000) AS shipped_tons, InventDim.InventLocationId 
        FROM ((InventTrans INNER JOIN InventTable ON InventTrans.ItemId = InventTable.ItemId) 
    INNER JOIN InventTransOrigin ON InventTrans.InventTransOrigin = InventTransOrigin.recId) 
    INNER JOIN InventDim ON InventTrans.InventDimId = InventDim.InventDimId 
    WHERE InventTrans.dataAreaId = ':daxEntity' and InventTrans.InventTransOrigin > ' ' and InventTrans.ItemId > ':firstItem' and 
Abs(InventTrans.Qty) > 0 and InventTrans.DatePhysical >= '10/16/2019' and InventTrans.DatePhysical <= '10/17/2019' and
 InventTrans.statusissue = 2 and InventTable.dataAreaId = ':daxEntity' and InventTransOrigin.dataAreaId = ':daxEntity' and
 InventTransOrigin.ItemId = InventTrans.ItemId and InventTransOrigin.ReferenceCategory = '0' and 
InventDim.dataAreaId = ':daxEntity' Group BY InventDim.InventLocationId";
                 try{
                        sqlConn = DriverManager.getConnection(sqlurl, sqluser, sqlpassword);
                        System.out.println("Connected to the server successfully.");
                        PreparedStatement preparedStatement;
                        stmt = sqlConn.createStatement();
                        ResultSet resultSet = stmt.executeQuery(SQL_SELECT);
                        int colCount = resultSet.getMetaData().getColumnCount();
                        int direction = resultSet.getFetchDirection();
                        System.out.println(colCount);
                        System.out.println(direction);
                        int loop = 0;
                        while(resultSet.next()){
                        System.out.println("While start");
                            for(int i = 1; i <= colCount; i++){
                                    System.out.println(loop + " " + resultSet.getMetaData().getColumnName(i) + " " + resultSet.getObject(i).toString());

                            }
                                loop++;
                        }

                        System.out.println("While ENDED");

                    }catch (SQLException e) {
                        System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
                    } catch(Exception e){
                        e.printStackTrace();
                    }

                    return sqlConn;
                }

I tried to get the count of rows simply by using .last() then getRow() but I get this error when trying that. What am I doing wrong? This query runs great in our old in house c++ application but I'm having issues now that I'm trying to update them as Java apps.

Code:

ResultSet resultSet = stmt.executeQuery(SQL_SELECT);
        int colCount = resultSet.getMetaData().getColumnCount();
        int direction = resultSet.getFetchDirection();
        resultSet.last();
        int rows = resultSet.getRow();
        System.out.println(colCount);
        System.out.println(rows);

Output:

run:
Connected to the server successfully.
SQL State: null
The requested operation is not supported on forward only result sets.BUILD SUCCESSFUL (total time: 1 second)
  • Right now I just want to figure out how to print the results of the query with system.out. Is having the metadata call in the for loop causing the issue? I used a prepared statement first with the same result. So I tried statement and callablestatement. I had the tablenames renamed using initials but went back and dedicated their names to make sure it wasn’t that. Ignore the structure and query if you can as this is after hours of frustration and tweaking this or that to try and get the result. Is the call for the column names what is causing it to attempt more than a one way trip? – BEW1989 Oct 17 '19 at 01:25
  • 1
    You cannot call [`last()`](https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html#last--) on a forward-only result set, as documented in its apidoc. Nor should you, it is rather wasting resources (time, memory and IO) to materialize an entire result set to just get its count. – Mark Rotteveel Oct 17 '19 at 06:49

0 Answers0