Sunday, 26 August 2012

What happen when Class.forName("com.mysql.jdbc.Driver"); executes

Hi,

Several times we saw the code like this:

Class.forName("com.mysql.jdbc.Driver");
  Connection connection = null;
  connection = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","uname", "pass");

When Class.forName(com.mysql.jdbc.Driver") executes
then it will search for the Driver class in package com.mysql.jdbc.
If it finds then  it loads the Driver class and instantiate it,
otherwise it will throw the "ClassNotFoundException".

During instantiation it will execute the static block of that "Driver class" which will register it to the "DriverManager" as under:

static
    {
        try
        {
            DriverManager.registerDriver(new Driver());
        }
        catch(SQLException E)
        {
            throw new RuntimeException("Can't register driver!");
        }
    }

This registration is important because when "connection = DriverManager.getConnection(
          "jdbc:mysql://hostname:port/dbname","username", "password");" executes then it  tries to make the database connection using that Driver.

Eg"- If we try to make the connection to the SQLdatabase and we write the Class.forName("oracle.jdbc.OracleDriver"); then it does not make the connection.
For connection to the SQL DB we must have registered the SQL Driver.
This happens to every database like Oracle, SQL, Microsoft, hsql, derby etc.

And we can create db connections to different database as under:

Class.forName("oracle.jdbc.OracleDriver"); 
Class.forName("com.mysql.jdbc.Driver");
 Connection sqlConnection = null;
 Connection oracleConnection = null;


sqlConnection = DriverManager.getConnection(
          "jdbc:mysql://hostname:port/dbname","username", "password");"


oracleConnection = DriverManager.getConnection(
          "jdbc:oracle://hostname:port/dbname","username", "password");"

In the above code, DriverManager tries to make the connection by using all the drivers registered, if using the first driver the connections is not established, then it tries the next one until the connection is established or the drivers are finished.

Hope this information is helpful.

Regards
Rajan Bansal





Making of executable jar by using "jar" utility in jdk

Hi,

We can make the jars and executable jars by using the  "jar" utility present in the jdk as under:

1. Create the class having the main() method in package say main.pckage.MainClass.java
2. Create another class having any method in package say com.other.pckage.OtherClass.java.
3. Compile both classes.
4. Create a text file say manifest.txt and write the Main-Class: main.pckage.MainClass
5. Now we have two main folder main and com containing the .class files and one manifest.txt file.
6. Put all these in one folder say "Executable Jar".
7. I assume that you set the java path variable.
8. Open the command prompt and paste the
cmd-->  C:\Users\Rajan Bansal\Desktop\Executable Jar>jar cvfm myjar.jar manifest.txt com
/other/pckage/*.class main/pckage/*.class
 and the output is:
cmd--> added manifest
adding: com/other/pckage/OtherClass.class(in = 547) (out= 344)(deflated 37%)
adding: main/pckage/MainClass.class(in = 754) (out= 454)(deflated 39%)

Now we have successfully created the executable jar file in cmd.

Now write cmd--> java -jar myjar.jar
It will execute the main method of MainClass.java

And we can use that jar in any of our projects.

For more info click here..