Top Ten Jdbc Best Practices For Coffee Programmer
Java JDBC Best practices
JDBC Best Practices are roughly coding practices which Java programmer should follow spell writing JDBC code. As discussed in how to connect to Oracle database from Java, JDBC API is used to connect as well as interact amongst a Database management System. We own got touched roughly of the JDBC best practices inwards our lastly article 4 JDBC Performance tips, On which nosotros own got discussed elementary tips to improve surgical procedure of Java application amongst database. By using JDBC yous tin execute DDL, DML as well as Stored Procedures. JDBC Best practices is in all likelihood most pregnant laid of coding practices inwards Java because it significantly deport upon surgical procedure of Java application. I own got seen substantial surgical procedure attain past times but next mutual JDBC best practices similar running queries amongst machine commit vogue disable. One of the query which nosotros used inwards our illustration of JDBC Batch update was taking around xxx 2nd to destination amongst machine commit vogue enabled but it precisely took nether ane 2nd amongst machine commit vogue disable as well as using explicit commit. This JDBC tutorial is collection of such practices which assistance yous to write ameliorate JDBC code as well as inwards most cases lawsuit inwards improved performance.
10 JDBC Best pratices inwards Java
Here is my listing of exceed 10 JDBC best practices inwards Java which helps to avoid potential error, to larn ameliorate surgical procedure as well as helps to write robust Java database connection code. JDBC Best Practice #1: Use PreparedStatement
This is past times far most pop JDBC practices suggested past times everyone who has worked inwards JDBC API inwards Java. Indeed PreparedStatement deserve that admiration because of useful services it provides similar prevention from SQL injection, Precompiled SQL queries as well as role of bind variables every bit discussed in why Use PreparedStatement inwards Java
JDBC Best Practice #2: Use ConnectionPool
ConnectionPool every bit JDBC best exercise has already gained recognition as well as it fifty-fifty larn criterion at nowadays days. Several framework provides inwards built connection Pool facility like Database Connection Pool inwards Spring, DBCP as well as if yous are running inwards managed environs similar J2EE Application Server e.g. WAS or JBOSS, Server volition render Connection Pool facility. rational behind this JDBC best practices is that Creating JDBC connection accept relatively longer fourth dimension which tin growth overall response time, past times caching JDBC connection inwards puddle application tin instantly access database.
JDBC Best Practice #3: Disable machine commit mode
This is ane of those JDBC best practices which provided substantial surgical procedure attain inwards our JDBC batch update example. Its recommended to run SQL query amongst machine commit vogue disable. Rational behind this JDBC best exercise is that amongst machine commit vogue disabled yous tin group SQL Statement inwards ane transaction spell inwards instance of machine commit vogue every SQL disceptation runs inwards its ain transaction as well as committed every bit shortly every bit it finishes. So e'er run queries amongst machine commit vogue disabled
JDBC Best Practice #4: Use JDBC Batch Update
This is roughly other JDBC best exercise which is rattling popular. JDBC API provides addBatch() method to add together SQL queries into batch as well as executeBatch() to post batch queries for execution. Rational behind this JDBC best practices is that, JDBC batch update potentially trim down discover of database roundtrip which lawsuit inwards pregnant surgical procedure gain. So e'er Use JDBC batch update for insertion as well as update queries.
JDBC Best Practice #5: Access ResultSet using column get upwards to avoid invalidColumIndexError
JDBC API allows to access information returned past times SELECT query using ResultSet, which tin farther live accessed using either column get upwards or column index. This JDBC best exercise advise using column get upwards over column index inwards lodge to avoid InvalidColumnIndexException which comes if index of column is incorrect, most mutual of them is 0, since ResultSet column Index starts from 1, nil is invalid. Also yous don't need to alter your JDBC access code if lodge of column changed inwards SELECT SQL query, which is a major maintenance attain as well as a robust agency to write JDBC code. Some Java programmer may combat that accessing column using index is faster than name, which is truthful but if yous hold off inwards damage of maintenance, robustness as well as readability, I prefer accessing column using get upwards inwards ResultSet Iterator.
JDBC Best Practice #6: Use Bind variables instead of String concatenation
In JDBC Best Practice #1 nosotros own got advise to use PreparedStatement inwards Java because of ameliorate performance. But surgical procedure tin solely live improved if yous role bind variables denoted past times ? or house holders. which allows database to run same query amongst dissimilar parameter. This JDBC best practices also lawsuit inwards ameliorate surgical procedure as well as also render protection against SQL injection.
JDBC Best Practice #7: Always unopen Statement, PreparedStatement as well as Connection.
Nothing novel on this JDBC Best practice. Its mutual Java coding exercise to unopen whatever resources inwards finally block every bit shortly every bit yous are done amongst that. JDBC Connection as well as other JDBC classes are costly resources as well as should live closed inwards finally block to ensure unloosen of connection fifty-fifty inwards instance of whatever SQLException. From Java vii onwards yous tin role Automatic Resource Management (ARM) Block to unopen resources automatically.
JDBC Best Practice #8: Choose suitable JDBC driver for your application
There are iv typs of JDBC driver inwards Java as well as it tin withdraw deport upon the surgical procedure of DAO layer. e'er role latest JDBC Driver if available as well as prefer type iv native JDBC Drivers.
JDBC Best Practice #9: Use criterion SQL disceptation as well as avoid using db specific query until necessary
This is roughly other JDBC best exercise inwards Java which ensures writing portable code. Since most of JDBC code is filled upwards amongst SQL query its slowly to starting fourth dimension using Database specific characteristic which may introduce inwards MySQL but non inwards Oracle etc. By using ANSI SQL or past times non using DB specific SQL yous ensure minimal alter inwards your DAO layer inwards instance yous switch to roughly other database.
JDBC Best Practice #10: Use right getXXX() method
This is the lastly JDBC best exercise inwards this article which advise using right getter spell getting information from ResultSet to avoid information conversion fifty-fifty though JDBC allows to larn whatever information type using getString()or getObject().
That's all on JDBC best practices for Java Programmer, I am certain in that place are many to a greater extent than JDBC best practices around but these are most mutual practices which I tin mean value of. allow us know if yous are familiar amongst whatever other JDBC best practice.
Further Learning
JSP, Servlets as well as JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 as well as 2
10 best practices to follow spell writing code comments
Komentar
Posting Komentar