Search code examples
javaoraclerft

Need advice on fixing my JAVA query statement?


My JAVA script consists of 2 JAVA classes: RMS, queryRMS In the RMS class I call the method in the queryRMS class

RMS Java Class (I left out the start execution part, below is just the method)

    for (int i = 1; i <= itemCount; i++) {
        GlobalVariables.numberRow = i;
        JavaDatapool.settings();

        String item = queryRPM.connectDB_Multi(configFile,"SELECT ITEM FROM ORDSKU WHERE ORDER_NO  = '" + orderNo + "' ORDER BY ITEM ASC",i);
        JavaDatapool.writeXLS("item",item,GlobalVariables.sheetXLS);
        sleep(1);

    }

queryRMS JAVA class

public static String connectDB_Multi(String configFile, String query, int i) throws FileNotFoundException, IOException, SQLException, ClassNotFoundException{
    Properties p = new Properties();
    p.load(new FileInputStream(configFile));

    String serverName = (p.getProperty("RMS_DBServerName"));
    String portNumber = (p.getProperty("RMS_PortNumber"));
    String sid = (p.getProperty("RMS_SID"));
    String url = "jdbc:oracle:thin:@//" + serverName + ":" + portNumber + "/" + sid;
    String username = (p.getProperty("RMS_Username"));
    String password = (p.getProperty("RMS_Password"));
    //  jdbc:oracle:thin:@//localhost:1521/orcl

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection(url,username,password);     
    String setr = null;
    try {      
        Statement stmt = connection.createStatement();

        try {ResultSet rset = stmt.executeQuery(query);
            try {
                while(rset.absolute(i))   
                    setr = rset.getString(1);
                    return setr;  
            }        
            finally {
                try { rset.close(); 
                } 
                catch (Exception ignore) {}

            }
        } 
        finally {
            try { stmt.close(); 
            } 
            catch (Exception ignore) {}
        }
    } 
    finally {
        try { connection.close(); 
        } 
        catch (Exception ignore) {}

    }
}

So what it does is call the connectDB_multi class and then returns the String where the next part is saving it inside an Excel worksheet.

The loop should return all rows, one at a time and then save it inside the Excel worksheet.

In the second time in loop the query is faulted, eventhough the query should return 1 column consisting of 2 rows.

the original contained the part while(rset.next()) instead of while(rset.absolute(i)) but next only return the first row everytime. so the script works when only one column and row is retrieved from the Database.


Solution

  • Your logic looks a bit messed up.

    Look at the first loop you posted. You are, effectivly, executing:

    SELECT ITEM FROM ORDSKU WHERE ORDER_NO = '" + orderNo + "' ORDER BY ITEM ASC

    itemCount number of times. Each time you execute it, you are attempting to access the n:th row, n being loop counter. Do you see a problem there? How do you know that the query will return itemCount number of rows? Because if it doesn't, it will fail since you are attempting to access a row that doesn't exist.

    What I suspect you WANT to do is something like this

    Statement stmt = connection.createStatement();
    ResultSet rset = stmt.executeQuery(query);
    while(rset.next()) {
        JavaDatapool.writeXLS("item",rset.getString(1),GlobalVariables.sheetXLS);
    }
    

    You should also seriously consider using some form of connection pooling to avoid having to re-open new connections all the time as that is a pretty time-consuming operation.