Eve
Eve

Reputation: 514

Java converting a cell to string

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.

Upvotes: 0

Views: 1332

Answers (3)

Eve
Eve

Reputation: 514

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) {}

    }
}

Upvotes: 0

hkn
hkn

Reputation: 1448

Change

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

to

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

and remove

return rset;

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

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

Upvotes: 2

Related Questions