1

I am attempting to print some results from an SQL statement inside of Java and I am receiving an error saying these variables need to be initialized but I'm not sure how to go about doing this other than setting them to null which isn't what I want to do here, my error is when attempting to close st, rs and con, I am prompted to initialize the variables but this just sets them to null, here is my code so far:

public static void copyFolder(File src, File dest)
    throws IOException {
    Connection con;
    Statement st;
    ResultSet rs;

    try {
        con = DriverManager.getConnection("removed as it houses sensitive data");
        st = con.createStatement();
        String s = "SELECT Code FROM dbo.\"FK Facades$Dimension Value\" WHERE [Dimension Code] = 'PROJECT'";
        rs = st.executeQuery(s);
        while (rs.next()) {
            rs.getString(1);
            System.out.print(rs);
        }
    } catch (Exception e) {
        System.out.println("No connectarino");
    } finally {
        try {
            st.close();
            rs.close();
            con.close();
        } catch (Exception e) {
            System.out.println("No");
        }
    }
}
1
  • Consider using try-with-resources to manage connection, statements and result sets Commented Jun 28, 2016 at 13:37

2 Answers 2

2

In addition to what is already said, you should start using try-with-resources like this:

    try (Connection con = DriverManager.getConnection("removed as it houses sensitive data");
         Statement st = con.createStatement();
         ResultSet rs = st.executeQuery("...")) {
        // your code here
    }

This automatically closes the instances for you, and even gets the corner cases (i.e. exception in close()) right.

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

1 Comment

Thank you very much, I have implemented this into my code with the relevant changes and I am now receiving no errors, still won't connect to the database which could be for a totally different reason but thanks once again for solving this issue!
1

You need to initialize con, st and rs because if for example the first line in the try block fails, you will have no values for these variables so it cannot execute the finally block that is the reason why you get this compilation failure.

So it should be:

Connection con = null;
Statement st = null;
ResultSet rs = null;

...
try {
    if (rs != null)
        rs.close();
    if (st != null) 
        st.close();
    if (con != null)
        con.close();
} catch (Exception e) {
    System.out.println("No");
}

NB: Close the result set before the statement

3 Comments

Yes I did try to set each variable to null without much luck, will try the other part of the statement now, thanks
I tested on my side, I don't have any compilation error anymore with what I propose in my answer
I attempted to implement your suggestion but it was causing my other errors further down in my code with else statements, I've now fixed this using the first answer using try-with-resources, thanks for your help though!

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.