0

I'm learning to use MySQL at the moment and my instinct for instantiating and initializing my Connection and Statement objects was as following:

public class ProjectDriver {
    public static void main(String[] args)
    {
        Connection conn = null;
        Statement stmt = null;
        Initializer.sqlInitialize(conn,stmt);
    ...
    }
...
}

sqlInitialize:

public static void sqlInitialize(Connection conn, Statement stmt) {
    try {
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stmt = conn.createStatement();
    } 
    catch (SQLException se) {
        se.printStackTrace();
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
...
}

as well as an sqlClose method to close the Connection and Statement later on.

My instinct says that the Connection and Statement objects in the ProjectDriver should be properly initialized to their respective forms after the sqlInitialize method has been run, but that most certainly isn't the case. Eclipse has a null-pointer access warning in the IDE, and a NullPointerException error is thrown upon trying to run stmt.ExecuteUpdate(sql);.

I'm pretty sure this is a stupid question, but I can't figure out how to Google (or inquire of StackOverflow) why this is happening. I know that the code I provided isn't really the proper way to do what I'm doing, but I'm curious as to why this is happening. My understanding of pass-by-reference vs. pass-by-value in Java is that all primitives are pass-by-value, and that all Objects are pass-by-reference. Shouldn't modifications made to the Connection and Statement objects in another class remain, even after returning to the main?

Eli K
  • 39
  • 7
  • By the way, learn about try-with-resources syntax in modern Java, to close your `Connection` and such. See [Tutorial](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). – Basil Bourque Feb 12 '20 at 17:29
  • @BasilBourque Would it be inappropriate to create and close the `Connection` and `Statement` in a try-with-resources block, and then simply call a ~few dozen methods from within that t-w-r block? – Eli K Feb 13 '20 at 17:04
  • You could do that. The resources contained in the parentheses are not closed until the `{…}` block completes (or an exception is thrown). You can also nest a try-with-resources inside another try-with-resources. Caveat: I cannot imagine why you would have dozens of methods called from within a JDBC try-with-resources block. Most of the time you should have a short amount of code that exchanges data with the database, and exit the try-with-resources (closing your resources) — then move on to working with the data *after* the try-with-resources. But there are always exceptions. – Basil Bourque Feb 13 '20 at 22:36
  • @BasilBourque This is for an A.P. Research project. I need to generate... a lot of data, in a couple of permutations of several forms of numerical generation; there will be around a hundred and forty total tables when the project is done, but I'm making an effort to follow conventions so that I have at least one significant project completed to convention standards by the time I graduate high school. If the nested try-block doesn't include declarations or instantiations, that would simply make it a regular try block, correct? – Eli K Feb 14 '20 at 15:52
  • See the Question: [*How should I use try-with-resources with JDBC?*](https://stackoverflow.com/q/8066501/642706), and especially [my Answer](https://stackoverflow.com/a/60233458/642706). Notice the nested try-with-resources as typically useful in JDBC work where we generate a `ResultSet` only after having defined the `Connection` and `PreparedStatement` resources earlier. – Basil Bourque Feb 14 '20 at 21:15

2 Answers2

0

Java "passes references by value".

If you assign a parameter within a method, then that doesn't affect the caller. However, if you modify the object pointed to by the parameter, then every variable that refers to that object will see the modification (within the limitations of the memory model if multl-threaded).

For handling resources, such as JDBC Connection and Statement you will generally want to use the Execute Around idiom.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Thank you, this is what I was looking for. I wasn't sure what question to ask, this is what I needed to know. – Eli K Feb 13 '20 at 16:25
0

Java Programming Language does not support pass-by-reference. JDBC is a Java Based API.

  • Other programing languages use pass by reference or pass by a pointer. But in Java no matter what type of argument you pass the corresponding parameter (Primitive variable or Object) will get a copy of that data. If a calling a method passes a reference will be pointing to the same object – Namal Dinesh Ubhayawardana Feb 12 '20 at 17:43
  • I'm aware that java is technically "pass by value", but the value passed is a pointer to the object. In working with - for instance, ArrayLists - the behavior that I had observed was modifying an object in a method called would modify the original object. I was confused as to why, in this case, instantiating the object in a method called does not keep it instantiated. – Eli K Feb 13 '20 at 17:05