//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
Post a Comment