Java JDBC Best practices
JDBC Best Practices are some coding practices which Java programmer should follow acre writing JDBC code. As discussed in how to connect to Oracle database from Java, JDBC API is used to connect together with interact amongst a Database administration System. We own got touched some of the JDBC best practices inwards our lastly article 4 JDBC Performance tips, On which nosotros own got discussed uncomplicated tips to improve functioning of Java application amongst database. By using JDBC y'all tin execute DDL, DML together with Stored Procedures. JDBC Best practices is in all likelihood most pregnant laid of coding practices inwards Java because it significantly impact functioning of Java application. I own got seen substantial functioning attain past times only next mutual JDBC best practices similar running queries amongst machine commit trend disable. One of the query which nosotros used inwards our instance of JDBC Batch update was taking nigh xxx instant to destination amongst machine commit trend enabled but it precisely took nether 1 instant amongst machine commit trend disable together with using explicit commit. This JDBC tutorial is collection of such practices which assist y'all to write ameliorate JDBC code together with inwards most cases number inwards improved performance.
10 JDBC Best pratices inwards Java
Here is my listing of transcend 10 JDBC best practices inwards Java which helps to avoid potential error, to larn ameliorate functioning together with helps to write robust Java database connector 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 together with travel of bind variables every bit discussed in why Use PreparedStatement inwards Java
JDBC Best Practice #2: Use ConnectionPool
ConnectionPool every bit JDBC best practise has already gained recognition together with it fifty-fifty conk measure at nowadays days. Several framework provides inwards built connector Pool facility like Database Connection Pool inwards Spring, DBCP together with if y'all are running inwards managed environs similar J2EE Application Server e.g. WAS or JBOSS, Server volition furnish Connection Pool facility. rational behind this JDBC best practices is that Creating JDBC connector accept relatively longer fourth dimension which tin increase overall response time, past times caching JDBC connector inwards puddle application tin at nowadays access database.
JDBC Best Practice #3: Disable machine commit mode
This is 1 of those JDBC best practices which provided substantial functioning attain inwards our JDBC batch update example. Its recommended to run SQL query amongst machine commit trend disable. Rational behind this JDBC best practise is that amongst machine commit trend disabled y'all tin group SQL Statement inwards 1 transaction acre inwards instance of machine commit trend every SQL contestation runs inwards its ain transaction together with committed every bit shortly every bit it finishes. So ever run queries amongst machine commit trend disabled
JDBC Best Practice #4: Use JDBC Batch Update
This is some other JDBC best practise which is really popular. JDBC API provides addBatch() method to add together SQL queries into batch together with executeBatch() to post batch queries for execution. Rational behind this JDBC best practices is that, JDBC batch update potentially cut down number of database roundtrip which number inwards pregnant functioning gain. So ever Use JDBC batch update for insertion together with update queries.
JDBC Best Practice #5: Access ResultSet using column advert to avoid invalidColumIndexError
JDBC API allows to access information returned past times SELECT query using ResultSet, which tin farther live accessed using either column advert or column index. This JDBC best practise propose using column advert over column index inwards social club to avoid InvalidColumnIndexException which comes if index of column is incorrect, most mutual of them is 0, since ResultSet column Index starts from 1, cipher is invalid. Also y'all don't need to alter your JDBC access code if social club of column changed inwards SELECT SQL query, which is a major maintenance attain together with a robust means to write JDBC code. Some Java programmer may debate that accessing column using index is faster than name, which is truthful but if y'all await inwards price of maintenance, robustness together with readability, I prefer accessing column using advert inwards ResultSet Iterator.
JDBC Best Practice #6: Use Bind variables instead of String concatenation
In JDBC Best Practice #1 nosotros own got propose to use PreparedStatement inwards Java because of ameliorate performance. But functioning tin alone live improved if y'all travel bind variables denoted past times ? or house holders. which allows database to run same query amongst dissimilar parameter. This JDBC best practices also number inwards ameliorate functioning together with also furnish protection against SQL injection.
JDBC Best Practice #7: Always closed Statement, PreparedStatement together with Connection.
Nothing novel on this JDBC Best practice. Its mutual Java coding practise to closed whatever resources inwards finally block every bit shortly every bit y'all are done amongst that. JDBC Connection together with other JDBC classes are costly resources together with should live closed inwards finally block to ensure liberate of connector fifty-fifty inwards instance of whatever SQLException. From Java vii onwards y'all tin travel Automatic Resource Management (ARM) Block to closed resources automatically.
JDBC Best Practice #8: Choose suitable JDBC driver for your application
There are four typs of JDBC driver inwards Java together with it tin guide impact the functioning of DAO layer. ever travel latest JDBC Driver if available together with prefer type four native JDBC Drivers.
JDBC Best Practice #9: Use measure SQL contestation together with avoid using db specific query until necessary
This is some other JDBC best practise inwards Java which ensures writing portable code. Since most of JDBC code is filled upwards amongst SQL query its tardily to kickoff using Database specific characteristic which may acquaint inwards MySQL but non inwards Oracle etc. By using ANSI SQL or past times non using DB specific SQL y'all ensure minimal alter inwards your DAO layer inwards instance y'all switch to some other database.
JDBC Best Practice #10: Use right getXXX() method
This is the lastly JDBC best practise inwards this article which propose using right getter acre 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 to a greater extent than or less but these are most mutual practices which I tin intend of. allow us know if y'all are familiar amongst whatever other JDBC best practice.
Further Learning
JSP, Servlets together with JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 together with 2
10 best practices to follow acre writing code comments