Thursday 14 February 2008

How to make JDBC Connection? very simple :)

Before we can do any database related operation like queries or stored procedure calls, we first establish a connection to the database. Fundamentally there are 2 ways you can connect to database. Here I will talk about how we connect through JDBC type-IV drivers, because type-IV are what we use most of the time.
1) DriverManager
2) DataSource

Before we start our discussion on how to make a connection, first we make some setups right.
1) Make sure that you have appropriate JDBC driver available, which is generally a jar file. e.g. classes14.jar for oracle, jconnect.jar for sybase etc.
2) Make sure this driver jar file is in your classpath. e.g. in Eclipse you can add the jar file in your classpath like following

I am listing below the JDBC driver download locations. Please choose the appropriate one for your requirements if you don't have the one.











  • To add the JAR to the project classpath. Right-click on the project and select "Properties". Go to the "Libraries" tab in "Java Build Path" and click "Add External Jars" button



    Select the relevant driver jar Click "OK"



    Thats it!!!

    Defining the Connection URL is the key to connect to the database successfully. The connection URL basically consist of 3 parts
    a) The database hostname e.g. localhost
    b) The databse port number e.g. 1531
    c) additional info e.g. database name for mysql or serviceId for oracle.

    The connection URLs are different for different drivers and databases. you are adviced to always consult the driver documentation to know the connection URL formats.

    Generally the formats for different database drivers are follwoing.
    Oracle :
    JDBC Driver : oracle.jdbc.OracleDriver
    JDBC Connection URL : jdbc:oracle:thin:@<host_name>:<port_number>:<database_name>
    Click here for more help

    My-SQL :
    JDBC Driver : org.gjt.mm.mysql.Driver
    JDBC Connection URL : jdbc:mysql://<host_name>:<port_number>/<database_name>
    Click here for more Help

    Sybase :
    JDBC Driver : com.sybase.jdbc3.jdbc.SybDriver
    JDBC Connection URL : jdbc:sybase:Tds:<host_name>:<port_number>/<database_name>
    Click here for more Help

    MS-SQL :
    JDBC Driver : com.microsoft.jdbc.sqlserver.SQLServerDriver
    JDBC URL : jdbc:microsoft:sqlserver://<host_name>:<port_number>;DatabaseName=<database_name>
    Click here for more Help

    The next is the username/password information to establish the JDBC connection.
    Once we have all these things in place we can start with writing programs for database operations.

    A) JDBC Connection using DriverManager:
    1) Register the Driver: Registering the driver instructs JDBC Driver Manager which driver to load. We should know the driver class beforehand and I have listed a few already.
    We either use Class.forName() or DriverManager.registerDriver().
    2) Call DriverManager.getConnection(String connectionURL, String username, String password) to get the JDBC connection.

    The following sample code demonstrates how to register the driver and get the connection for different databases:

    MS-SQL :
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=databaseName", "userName", "password");

    Oracle :
    e.g. if serviceId is orcl (default) and port number is 1521
    DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:orcl", "scott", "tiger");

    My-SQL:
    Class.forName("org.gjt.mm.mysql.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://<host_name>:<port_number>/<database_name>", "sa", "sa");


    One of the biggest mistakes we make in our JDBC development is to hardcode configuration information. While everyone realize the fact but it does still happen. Another big mistake is to distribute similar setup information throughout your application, making it really hard to find all the places when you need to change or refactor.

    This seems to happen quite frequently with database drivers. There might be several instances of Class.forName("...") for loading the correct driver. In addition, the connection URL, username and password could be hardcoded or read from obscure config files. The desirable behaviour is to centralize such managements as far as possible.

    It is sometimes quite useful to know how long a database statement takes to complete and how frequently it is called. A great tool for this is JAMon. In the latest edition, they have support for monitoring JDBC calls. All that you need to do is use their JDBC driver, point it to your driver, and you're done. This is extremely easy when all the config is in one place, but if you are connecting to several databases in various locations in your codebase, it will require code changes. Ideally we should not need any code changes.

    B) JDBC Connection using DataSource:
    In-Progress

    Tuesday 12 February 2008

    Is Tomcat an application server?

    This is a long prevailing confusion whether Tomcat is an application server and always worth discussions.

    Tomcat is one of the most popular options for lightweight development scenarios, and in many cases meets the need for an application server, even though it is technically a Web server. In this article Jeff Hanson engages the question of whether Tomcat is an app server, first by explaining what differentiates application servers, Web servers, and Java EE containers, and then by evaluating Tomcat's suitability to a variety of common Java enterprise development scenarios.

    I read this very good article on Java World today, which explains the difference quite nicely, though every one may not agree with the author's opinion entirely. So its worth visiting the comments section as well.

    First the difference between app servers, Web servers, and Java EE containers are explained from JAVA EE point of view.

    Then some scenarios where a Web server like Tomcat could be used appropriately as an app server.
    demonstratons using scaled architecture, starting with the lightweight implementation where Tomcat shines, and concluding with a complex service-oriented architecture e.g. where you would be better off with a full-fledged Java EE application server.


    Read Full Article here.

    Here are the comments from visitors.

    Wednesday 6 February 2008

    Enhance Your Preparation With uCertify's New SCJP 6.0 Practice Test

    Enhance Your Preparation With uCertify's New SCJP CX-310-065 Practice Test

    Professionals preparing for the SCJP 6.0 exam can now study well with uCertify’s
    new CX-310-065 practice test.

    uCertify is providing a free evaluation version containing 30 questions.

    The exam tests your knowledge and understanding of the basic syntax and structure of the Java programming language. It also measures your skills in creating Java technology applications that run on server and desktop systems using Java SE 6.

    uCertify's Free Trial is available with 30 free Java Questions to download and 40 study notes

    60 questions SCJP Test Mock Exam. They are very close to the style of the the real Sun Certified Java Programmers exam. If you like them you can purchase a subscription to the all 320 from Author's website. Please follow the link: www.examulator.com/home/tigertamer.htm

    Sun's own e-practice exam provides some decent set of questions.

    Java Black Belt Series of Java 5 Questions. You can browse various questions Topic wise as well. Nice collection.

    One SCJP Question Each Day

    Monday 4 February 2008

    Watch Erik Meijer talking Functional Programming.

    Erik Meijer is talking about the functional programming languages, academic versus real-world application of functional programming languages. watch the video here

    http://channel9.msdn.com/ShowPost.aspx?PostID=374141