0

I have the following Java code: (the parameters are the login data aswell as the query for the database)

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

    String serverName = (p.getProperty("RMS_DBServerName"));
    String portNumber = (p.getProperty("DB_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"));

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection(url, username, password);

    try {
        Statement stmt = connection.createStatement();

        try {
            ResultSet rset = stmt.executeQuery(query);
            try {
                while (rset.next()) {
                    System.out.println(rset.getString(1));
                }
            } finally {
                try {
                    rset.close();
                } catch (Exception ignore) {
                }
            }
            return rset;
        } finally {
            try {
                stmt.close();
            } catch (Exception ignore) {
            }
        }
    } finally {
        try {
            connection.close();
        } catch (Exception ignore) {
        }
    }
}

The code is supposed to get the one cell from the database. The query works when the result is given to the console. But I want to return the cell from the method to another method in order to automate the process. But the return is returning code like this: jdbc:oracle:OracleDriver#........ . So can anyone help to fix this, I just want to return one cell that contains a number.

2
  • That's a mess to read, by the way--I'd consider wrapping some of that up in utility functions/etc. As it stands, its purpose is buried and difficult to discern. Commented Oct 21, 2011 at 16:27
  • Your method is defined to return a String, but yet you are returning a (closed!) ResultSet. Does not make any sense to me. Commented Oct 21, 2011 at 16:43

3 Answers 3

2

Instead of printing the string, return it. Right now you're returning the string representation of the result set.

Sign up to request clarification or add additional context in comments.

1 Comment

that is: return rset.getString(1) in lieu of return rset
1

Change

while (rset.next())
         System.out.println(rset.getString(1));
}

to

if(rset.next()) {
     return rset.getString(1);
}

and remove

return rset;

4 Comments

I changed the statement but now it's telling me to add a return when it's clearly now in the code or to change to void but then i have to remove the return
add return null; to the end of the method
You need a return in every execution path. So return null or an empty string in an else block.
I added the return null and now it returns NULL so I don't get any data back anymore
0

I solved the problem this the code I got

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

    String serverName = (p.getProperty("RMS_DBServerName"));
    String portNumber = (p.getProperty("DB_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"));

    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.next())   
                    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) {}

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.