Introduction To JDBC

Feb 07

This tutorial aims to give you a quick intro to Java Database Connectivity (JDBC) API. JDBC technology was designed to keep simple things simple. This means that the JDBC API makes everyday database tasks, such as simple SELECT statements, very easy. After this you should be able to use the API to do basic and some advanced Database related tasks.
I will use MySQL for this tutorial but you can use whatever DBMS you want. Before starting you should download the appropriate JDBC driver based on your database selection. The driver should be available on the database site with the instructions for installing it. However the installation process will be (in most cases) adding the driver JAR to the project path and importing it.
You can download MySQL JDBC driver from here

  • Loading Drivers
  • Making a Connection
  • This step aims to make a connection to the database you will work on. The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

    If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers. If the database is not specified, the connection will be made with no default database. In this case, you will need to either call the set- Catalog() method on the Connection instance or fully specify table names using the database name (that is, SELECT db- name.tablename.colname FROM dbname.tablename…) in your SQL

    Now, in your project this is how you make a connection to the database named TestJDBC (you should create it before trying to use it).

  • Creating a Table
  • A Statement object is used for executing a static SQL statement and returning the results it produces. Simply you create a Statement and then execute it. You have couple of options when executing a Statement:

    1. For a SELECT statement, the method to use is executeQuery.
    2. For statements that create or modify tables, the method to use is executeUpdate.

    NOTE: Unlike DBMS, the Java compiler care about where lines are divided, and it will not compile a String object that extends beyond one line. So, when you are giving strings in the Java programming language, you need to enclose each line in quotation marks and use a plus sign (+) to concatenate them.

  • Entering Data into a Table
  • Getting Data from a Table
  • A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

  • Prepared Statements & Variable based queries
  • A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

  • Using Joins
  • Transactions
  • If you don’t know what is meant by transaction you may want read this from Wikipedia first Database transaction.
    JDBC Connection created with auto-commit mode on, so you need to switch it off, do your work, then commit it.

  • Scrollable Result Set
  • Unlike the regular ResultSet, in a scrollable ResultSet object, the cursor can be moved backwards and forwards, to an absolute position, or to a position relative to the current row.

    NOTE: I encourage you to go to the official API and read more about this. ResultSet Official API

If you want the eclipse project that contains all this code you can download it from Here

5 comments

  1. I can see you are an expert at your field! I am launching a web site soon, and your facts will be very useful for me.. Thanks for all your assist and wishing you all the success.

  2. Cool post. Hopin’ for more. 🙂

  3. hey 🙂 what design are you utilizing for this one in particular? (this actual blog) I kinda love this and I was questioning if this can be accessible.

Leave a Reply