
Efficient JDBC Usage and Best Practices
Learn about the differences between Statement and PreparedStatement in JDBC, the importance of properly closing JDBC resources, the efficiency of PreparedStatement for repeated queries, the risks of sharing connections among threads, and more essential tips for effective JDBC usage.
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Statement vs. PreparedStatement PreparedStatement more efficient when query executed repeatedly (possibly with different parameter values) the server creates the execution plan only once. Not always depends on implementation Statement threat of SQL injection ts = stmt.executeQuery( "select id, firstname, lastname, salary " + " from employee where id=" + "3; DELETE FROM empkoyee );? ?
Closing JDBC resources conn.close() may not close the connection if the connection has some JDBC resources open. Don t rely on assumption that conn.close() should close them !!! => rs.close(); stmt.close(); conn.close()
Comparison of access methods No? of? queries Response? in? ms Method Statement PreparedStatement? with? closing PreparedStatement? w/o? closing Statement PreparedStatement? with? closing PreparedStatement? w/o? closing Statement PreparedStatement? with? closing PreparedStatement? w/o? closing 82 84 43 435 271 128 1663 1358 1027 100 1000 10000
Connection and ResultSet Don t share connection among multiple threads ResultSet rs = stmnt.executeQuery(); rs may reference an object owned by the JDBC driver => danger of a deadlock Another reason why connection shall not be shared: The transaction is bound to the connection