Top Ten Jdbc Best Practices For Coffee Programmer
Java JDBC Best practices
JDBC Best Practices are some 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 too interact amongst a Database administration System. We lead keep touched some of the JDBC best practices inward our in conclusion article 4 JDBC Performance tips, On which nosotros lead keep discussed unproblematic tips to improve surgical physical care for of Java application amongst database. By using JDBC y'all tin forcefulness out execute DDL, DML too Stored Procedures. JDBC Best practices is in all likelihood most pregnant laid of coding practices inward Java because it significantly comport upon surgical physical care for of Java application. I lead keep seen substantial surgical physical care for hit past times but next mutual JDBC best practices similar running queries amongst motorcar commit agency disable. One of the query which nosotros used inward our representative of JDBC Batch update was taking well-nigh thirty minute to destination amongst motorcar commit agency enabled but it exactly took nether i minute amongst motorcar commit agency disable too using explicit commit. This JDBC tutorial is collection of such practices which assist y'all to write ameliorate JDBC code too inward most cases effect inward improved performance.
10 JDBC Best pratices inward Java
Here is my listing of move past times 10 JDBC best practices inward Java which helps to avoid potential error, to instruct ameliorate surgical physical care for too helps to write robust Java database connecter code.
JDBC Best Practice #1: Use PreparedStatement
This is past times far most pop JDBC practices suggested past times everyone who has worked inward JDBC API inward Java. Indeed PreparedStatement deserve that admiration because of useful services it provides similar prevention from SQL injection, Precompiled SQL queries too purpose of bind variables every bit discussed in why Use PreparedStatement inward Java
JDBC Best Practice #2: Use ConnectionPool
ConnectionPool every bit JDBC best do has already gained recognition too it fifty-fifty instruct measure at nowadays days. Several framework provides inward built connecter Pool facility like Database Connection Pool inward Spring, DBCP too if y'all are running inward 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 connecter accept relatively longer fourth dimension which tin forcefulness out growth overall response time, past times caching JDBC connecter inward puddle application tin forcefulness out instantly access database.
JDBC Best Practice #3: Disable motorcar commit mode
This is i of those JDBC best practices which provided substantial surgical physical care for hit inward our JDBC batch update example. Its recommended to run SQL query amongst motorcar commit agency disable. Rational behind this JDBC best do is that amongst motorcar commit agency disabled y'all tin forcefulness out group SQL Statement inward i transaction spell inward representative of motorcar commit agency every SQL controversy runs inward its ain transaction too committed every bit before long every bit it finishes. So e'er run queries amongst motorcar commit agency disabled
JDBC Best Practice #4: Use JDBC Batch Update
This is some other JDBC best do which is really popular. JDBC API provides addBatch() method to add together SQL queries into batch too executeBatch() to post batch queries for execution. Rational behind this JDBC best practices is that, JDBC batch update potentially cut back expose of database roundtrip which effect inward pregnant surgical physical care for gain. So e'er Use JDBC batch update for insertion too 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 forcefulness out farther survive accessed using either column advert or column index. This JDBC best do propose using column advert over column index inward gild to avoid InvalidColumnIndexException which comes if index of column is incorrect, most mutual of them is 0, since ResultSet column Index starts from 1, cypher is invalid. Also y'all don't take away to modify your JDBC access code if gild of column changed inward SELECT SQL query, which is a major maintenance hit too a robust agency 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 inward damage of maintenance, robustness too readability, I prefer accessing column using advert inward ResultSet Iterator.
JDBC Best Practice #6: Use Bind variables instead of String concatenation
In JDBC Best Practice #1 nosotros lead keep propose to use PreparedStatement inward Java because of ameliorate performance. But surgical physical care for tin forcefulness out entirely survive improved if y'all purpose bind variables denoted past times ? or house holders. which allows database to run same query amongst dissimilar parameter. This JDBC best practices also effect inward ameliorate surgical physical care for too also render protection against SQL injection.
JDBC Best Practice #7: Always closed Statement, PreparedStatement too Connection.
Nothing novel on this JDBC Best practice. Its mutual Java coding do to closed whatsoever resources inward finally block every bit before long every bit y'all are done amongst that. JDBC Connection too other JDBC classes are costly resources too should survive closed inward finally block to ensure unloosen of connecter fifty-fifty inward representative of whatsoever SQLException. From Java vii onwards y'all tin forcefulness out purpose Automatic Resource Management (ARM) Block to closed resources automatically.
JDBC Best Practice #8: Choose suitable JDBC driver for your application
There are iv typs of JDBC driver inward Java too it tin forcefulness out straight comport upon the surgical physical care for of DAO layer. e'er purpose latest JDBC Driver if available too prefer type iv native JDBC Drivers.
JDBC Best Practice #9: Use measure SQL controversy too avoid using db specific query until necessary
This is some other JDBC best do inward Java which ensures writing portable code. Since most of JDBC code is filled upward amongst SQL query its tardily to get-go using Database specific characteristic which may introduce inward MySQL but non inward Oracle etc. By using ANSI SQL or past times non using DB specific SQL y'all ensure minimal modify inward your DAO layer inward representative y'all switch to some other database.
JDBC Best Practice #10: Use right getXXX() method
This is the in conclusion JDBC best do inward this article which propose using right getter spell getting information from ResultSet to avoid information conversion fifty-fifty though JDBC allows to instruct whatsoever 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 or too hence but these are most mutual practices which I tin forcefulness out intend of. allow us know if y'all are familiar amongst whatsoever other JDBC best practice.
Further Learning
JSP, Servlets too JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 too 2
10 best practices to follow spell writing code comments
Belum ada Komentar untuk "Top Ten Jdbc Best Practices For Coffee Programmer"
Posting Komentar