Advanced Java: 07
Q. Write a Java program to implement the use case of interactive balance enquiry.
Use Case 1: Enter account no: 1001 Use Case 2: Enter account no: 10001
Balance is Rs. 66022.0 Account does not exist.
---------------------------------------------------------------------------------------------------------------------------
Memory Representation of above program:
Explanation of above program:
JDBC driver first duty is establishing the connection. Driver is connection creator.
> javac BalanceEnquiryApplication.java // it compiles the code
> java BalanceEnquiryApplication
Java command goes to the operating system then OS calls the JVM. now JVM loads the .class file into RAM, after that JVM gives call to main method. accordingly, STACK AREA gets created for the main method and which remains present in main memory still main method is in execution.
Once main method completely executes then control goes to JVM. Now JVM will release memory of all the stack area.
Heap Area all the three objects 1. Connection object. 2. Statement Object and 3. Resultset object are there, and they become unreachable objects.
JVM internally keeps eyes on the Heap Area, and he observes there are three unreachable objects, so it automatically releases the memory of the objects, that is deallocation of memory. This is called Garbage Collection in JVM. Even if you don't close then also JVM will take care of this.
Q. How does database server know where the java program in the world is?
Ans: Client IP address and Port Number available in Socket inside database server as a client socket.
Now, imagine 100 of client connected to database server, then its duty of database server to maintain the client socket of each client. It's a burden on database server.
Now, assume if some java program without closing the database connection exit from the connection, then foolish (poor) database server still maintaining that socket of client.
if a greater number of such connections are there which ultimately blocks the real people who wants to connect the database server due to connection limit. So, until and unless you are not closing connection.close() in your java program database server maintains its client socket.
Q. Why to close the connection and statement object?
Ans: To release networking resources maintained by database server. when statement object created at database server some memory is allocated for SQL Statement from client side. So, once we done release that memory also that's why statement.close() instruction must be there in client-side java program.
Q. Why to close Resultset.close()?
Ans: The answer is as follows:
- When the java program submit statement to database server, it gets data from the Hard disk. after that it keeps that data in cache memory (Faste memory) and then send it to JDBC driver.
- Now, question comes to your mind is why do we need cache memory? So, as we know always hard disk operation are slower and time consuming (database is secondary memory slower 1000 times than cache memory) so; to improve the performance we keep data in cache memory. Cache memory is faster and costlier.
- To close this cache memory, we have to close resultSet object. So, this is called graceful exit.
- Note: if same query comes it fetch from cache memory for faster retrieval, if not it returns from the database.