Java Learners

I had always wanted to connect to database to the java , either by connecting it to Oracle database or MS – Access DB.

So here is my step by step tutorial to get a new database connection from scratch .

What all is needed is :

MS Access , JDK1.2 and above , JVM

Step 1 : Making a Database

Go to Control Panel à   administrative tools à  Data Sources (ODBC) as shown :

 

 

Double click on the same and the following screen can be seen:

 

 

 

The above shown ODBC Data Source Administrator shows all the possible data base that exist in the system.

Since we want to add a new database , click on Add button .

 

Entire list of Database drivers pops up which can be used as data sources. Now select the Driver do Microsoft Access (*.mdb) and click Finish.

 

 

Upon Clicking Finish , there will be a pop-up asking for details of Database as shown below :

 

The Data Source name is the name we will be using in our code , and the description.

Data source name is mandatory while description can be left as blank. If database is already created, then simply click on select, and map the database to the ODBC. After clicking select, just browse it to database file.

In case the database is to be created, as we are doing it , lets name it JDBCBasic and Description : Simplest DB connection.

 

As soon as we click Create , a pop-up comes asking for the location where the Database file is intended to be kept.

The data base file have an extension of mdb. In case one wants to change the drive, select the desired drive from dropdown Drives:

 Lets name it PLES. As soon we give the location of file , and pres Ok , there will be a confirmation pop up as :

 

So now DB is ready and is ready to be used.

 

Press Ok on every open pop-up , and move out.

 

Step 2 : Lets look at the Access DB just made .

 

The just created database shows up in the place it is just created.

 

The next step is to decide on what should be database. We will not go in details of database designing. Since my aim is to get a program ready and running in minimal time , lets open the database and create a table with few records in it.

 

Click on Options , and select the option , Enable this content

 

Since we are creating a new database from scratch , we can safely select the option.

Now we need to create the table.

Click on Create Tab , and select Table option.

 

 

Create table and fill in the columns , and select save.

 

  The table I have created is Colors.

 

And the fields are Id , Color and Suitable.

Id is the primary key and the unique identifier in the table.

 

Close the table and lets write the java code for the same.

 

Step 3 : writing java code

 

Here is the java code

package db;

 

import java.sql.*;

 

class JDBCTest{ 

  public static void main(String args[])

  {

  try{

/*

  * java.lang.Class extends Object implements Serializable.

  * forName(String name, boolean initialize, ClassLoader loader)

  *       Returns the Class object associated with the class or interface with

  *       the given string name, using the given class loader.

  *       Here since the driver is MS-Access , we are using JdbcOdbcDriver.

  */

 

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

/*

   * To create the connection the Database. Use the name of the Data Source used

   * to create the database here.

   * ("jdbc:odbc:JDBCBasic","USerId","Password")

   * Since UserId and Password were not specified at the time of creation of the

   * database, hence we have to leave the fields blank

   */

  Connection connection = DriverManager.getConnection("jdbc:odbc:JDBCBasic","","");

  Statement statement = connection.createStatement();

 

  ResultSet resultSet = statement.executeQuery("Select * from Colors");  // name of table

  connection.commit(); // Commit the query for any database connection

 

while(resultSet.next())   // To iterate the results of query

  {

    System.out.println(resultSet.getString(2));  

/* this is what is printed. Second column of table */

  } 

  }

  catch (Exception exception)

   {  System.out.println(exception); }   

   }

 }

   //***************************************************

 

 

The output of the code above is :

Red

Blue

Yellow

Orange

Conclusion : The document helps to create a database from scratch and connecting it to java code by using simple Jdbc driver.

Chapter 1Previous Chapter Home Page
Looking for Something more ... check the google
Comment Comment By Datetime
this is a good effort ship 3/16/2009 12:26:08 AM
Thanks Deepak 3/16/2009 8:45:28 AM

Drop a Comment

Comment
Name
Mail ID
Phone number