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)