Skip to main content

JDBC CODING STANDARDS


//explicit imports
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

//declaring class name
public class CodingStanderdConnTest {

      // declaring variables
      private static Connection connection = null;
      private static Statement statement = null;
      private static ResultSet resultset = null;

      // declaring main method
      public static void main(String[] args) {
            // outer try block for all statement
            try {
                  System.out.println("Execution started.....");
                  // handling ClassNotFoundException exception
                  try {
                        // loading sun.jdbc.odbc.JdbcOdbcDriver class and registering
                        // driver
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        System.out.println("sun.jdbc.odbc.jdbcodbcdriver  class loaded and registered");
                  } // closing inner try block which is related to loading class and
                        // register driver
                  catch (ClassNotFoundException e1) {
                        System.out.println("printing sun.jdbc.odbc.jdbcodbcdriver class related exception" + e1);
                  } // closing inner catch which is related to
                        // sun.jdbc.odbc.jdbcodbcdriver class

                  // handling connection related exception in inner try block
                  try {

                        // creating connection object
                        System.out.println("establishing connection process...........");
                        connection = DriverManager.getConnection("jdbc:odbc:oradsn");
                        System.out.println("Connection established....working ");

                  } // closing connection try block
                  catch (Exception e3) {
                        System.out.println("printing connection related exceptions :" + e3);
                  } // closing connection's (exception) catch block

                  // handling exception related to statement within try block
                  try {
                        // creating if statement
                        if (connection != null) {
                              System.out.println("creating statement object ...running.....");
                              // creating statement object
                              statement = connection.createStatement();
                              System.out.println("statement object created");
                        } // statement if closing
                  } // closing statement try block
                  catch (Exception e) {
                        System.out.println("printing exception related inner try block which is related to statement :" + e);
                  } // closing statement inner catch block

                  // Handling exception related to ResultSet object
                  try {
                        // creation condition for resultset, checking null
                        if (statement != null) {
                              // execution query
                              resultset = statement.executeQuery("select *from student");
                              System.out.println("ResultSet Object is created");
                        } // closing resultset if
                  } // closing resultset inner try block
                  catch (Exception e) {
                        System.out.println("printing exception related inner try block which is related to resultset :" + e);
                  } // closing resultset exception

                  // Retrieving data from database
                  if (resultset != null) {
                        while (resultset.next()) {
                              System.out.println("  " + resultset.getString(1) + "  " + resultset.getString(2) + "  "
                                          + resultset.getString(3));

                        } // closing while loop

                  } // closing if which is related to resultset
            } // closing outer try block which is catching all exceptions related to
                  // all objects

            // outer catch declaring here
            catch (Exception e) {
                  System.out.println("printing exception related outer try block  :" + e);
            } // outer catch closing
            finally {

                  if (resultset != null) {
                        // try block for handling resultset object
                        try {
                              // closing resultset object
                              resultset.close();
                              System.out.println("ResultSet Object closed");
                        } catch (Exception e) {
                              System.out.println("Exception is occurred during causing resultset object");
                        } // closing resultset closing try block
                  } // closing if of resultset

                  if (statement != null) {
                        // try block for handling closing statement object
                        try {
                              // closing statement object
                              statement.close();
                              System.out.println("Statement Object closed");
                        } catch (Exception e) {
                        } // closing catch block related to statement close object
                  } // closing if which is related statement

                  if (connection != null) {
                        // try block for handling closing connection object
                        try {
                              // closing connection object
                              connection.close();
                              System.out.println("Connection Object closed");
                        } catch (Exception e) {
                        } // closing connection related catch block
                  } // closing of if which is related to connection object closing
            } // closing finally block
      }// closing main

}// closing CodingStanderdConnTest class

Comments

Popular posts from this blog

Properties

Appendix A. Common application properties Various properties can be specified inside your  application.properties / application.yml  file or as command line switches. This section provides a list common Spring Boot properties and references to the underlying classes that consume them. Property contributions can come from additional jar files on your classpath so you should not consider this an exhaustive list. It is also perfectly legit to define your own properties. This sample file is meant as a guide only. Do  not  copy/paste the entire content into your application; rather pick only the properties that you need. # =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a guideline. Do NOT copy it in its # entirety to your own application. ^^^ # =================================================================== # ---------...

Java Questions

1.WHAT IS JAVA? Java is a very simple, high level, Secured, Multithreaded, Platform independent, Object Oriented Programing Language. It was invented by "Gems Gosling" in SUN MICROSYSTEM for developing internet applications. ========================================================== 2.WHY JAVA IS VERY SIMPLE? its syntaxes are similar to c/c++. it omits many of the features that makes c and c++ complex, confusing and unsafe. java has inbuilt rich set of programs (API) sothat project development fast and easy. Note: In java we don't implement most horrible topics such as pointers, structures, union, operator overloading, multiple classes inheritance and many more. ========================================================== 3.IS JAVA SUPPORTS POINTERS? Java doesn't supports pointer but internally pointers are there in java software [JVM]. Java native language is C that means java language is developed using C language so Java software can have pointers in...

Stellar Data Recovery

Stellar Data Recovery Tips - पानी में डूबी हुई हार्ड डिस्क से कैसे डेटा रिकवर करें पानी में डूबी हुई हार्ड डिस्क से कैसे डेटा रिकवर करें पिछले कुछ हफ्तों बारिश के रूप में प्रकृति का कहर अपने चरम पर था , जिसकी वजह से चेन्नई शहर में जीवन प्रभावित हुआ । भूकंप से लेकर भारी बाढ़ तक , देश में इस साल प्राकृतिक आपदा के बारे में निरंतर खबरें चलती रही । प्राकृतिक आपदाओं ने मानव जीवन पर एक भयंकर रोक सी लगा दी । प्राकृतिक आपदा के बाद व्यवसायों को भी भारी नुकसान भुगतना पड़ा - प्राकृतिक आपदा से न केवल फिज़िकल नुकसान हुआ लेकिन मूल्यवान डेटा सिस्टम भी प्रभावित हुए ।   हम सब जानते हैं , डिजिटल उपकरण के लिए पानी कितना खतरनाक हैं । हम हमेशा अपने गॅडजेट्स जैसे मोबाइल फोन , कैमरा , लैपटॉप इत्यादि को पानी से दूर रखने के लिए विशेष सावधानी बरतते हैं । लेकिन हम प्राकृतिक आपदाओं के मामले में असहाय हो जाते हैं , जैसे हाल ही में चेन्नई में जो हुआ । ...