JDBC with mySQL

The purpose of this Post is to get you beyond the initial hurdles involved in:

  • Use mySQL database for use with JDBC.
  • Writing and testing your first JDBC programs to administer the database and to manipulate the data stored in the MySQL database.

* What is JDBC:

JDBC technology is an API (included in both J2SE and J2EE) that provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files.

*  Connecting Java and mySQL

For Connecting Java and mySQL database you will need a Connector called “MySQL Connector/J” :

Download the Connector

What is MySQL Connector/J?

For those who don’t know, let me begin by explaining the purpose of MySQL Connector/J.

The JDBC API is designed to make it possible for you to write a single Java program and to use it to manipulate the data in a variety of different SQL database servers without a requirement to modify and/or recompile the program.  In order to do this, it is necessary for you to:

  • Inform the Java program as to the URL of the database server.  You can accomplish this with input data when you start the program.
  • Provide the Java program with a programming interface to the specific database server that you intend to use.  Assuming that the programming interface has been installed on your computer, you can also accomplish this with input data when you run the program.
Installing MySQL Connector/J :  First Extract the Connector/J downloaded archive.

It consists of many files. The file which is most imp for us is the jar file. May be named as:   “mysql-connector-java-5.1.12-bin.jar”

5.1.12 is the version, can be different for you. Depends on the version of Connector you download.

Now to install the Connector you will have 2 methods:

1. The following statement appears in the connector documentation Section 2.2.1. entitled Setting the CLASSPATH

copy the “com” and “org” subdirectories and all of their contents to anywhere you like, and put the directory holding the “com” and “org” subdirectories in your classpath

2. Second Method:

put mysql-connector-java-[version]-bin.jar in your classpath, either by adding the FULL path to it to your CLASSPATH environment variable, or by copying the .jar file to $JAVA_HOME/jre/lib/ext.

I will like to tell you the best method that worked for me and which is most uncomplicated is: Copy the downloaded Connector .jar file into /java/jre/lib/ext    folder. This easier rather typing commands or setting the classpath for different .jar files you need to run you program.

Note:  bfore running java programs on Windows Command prompt just do the following 2 steps and you can run the program from any directoy.

  • set path= “path of jdk/bin folder”    (without quotes) the path may be like(for Windows users)

C:\Program Files\Java\jdk1.5.0_14\bin

  • set classpath=%classpath%;.;

3. Lets check the connectivity using the Java Code:

Consider here the following things in the code:

  • database_name= The database you have made in mySQL you have to use
  • Database_password= Password of “root” in mySQL, you are using mySQL database as a ‘root’
  • The table you are using is “person” which has columns “name” and “age”
  • In the following code you take the name of person as command-line argument and use it to run the query
  • The command line argument is name of person you have in your database 🙂
  • Steps to run the following java code:
  • Save the following code and name the file as “example.java”
  • Run command on command prompt (Compile the code):    javac example.java
  • Execute the compiled File (Hope there are no Errors):  java example argument  (for example:    java example aniket      )

//Import Packages

import java.sql.*;
import java.lang.*;
public class example{
Connection con = null;
Statement smnt;
ResultSet rs;
String querystring;
public static void main(String[] args){

con = DriverManager.getConnection(DATABASE,USER,PASS);
System.out.println("MySQL server connected...");
smnt= con.createStatement();
querystring= "select age from person where name=\"" + args[0] + "\"";
rs = smnt.executeQuery(querystring);
System.out.println("The age is" + rs.getString("age"));
}//try Block Ends here
catch(Exception e) {System.err.println("Exception: " + e.getMessage());}
}//main ends here
}//class Ends

So the code is as above..
Here the post ends.. Hope this works for you..
While writing this post, I considered that you have Java(JDK) and mySQL configured on your computer.
Else this will not work for you.
Cheers… 🙂

Lucid Lynx — Ubuntu 10.04

After Every six months(April and October), I am waiting for release of the .04 and .10 version of Ubuntu Systems. I am always eager to see the updates in the updated versions. And so,  here I am waiting for the April Version of Ubuntu 10.04, named asLucid Lynx“.

So Lets take a look on to what are the features in Lucid Lynx: The new style in Ubuntu is inspired by the idea of “Light“.

New Logos:

1. Gnome 3 though awaited to be there in the release. Lucid still has Gnome 2.x version used in it. So there will not be many changes in the look and UI part. It is of course to be expected that Gnome 3 will have a lot of rough edges at its birth, which again doesn’t make it ideal in a LST release.

The upcoming Gnome 3 that is to be released early 2010, can already be tested in an early stage in Ubuntu 9.10. If you want to test the Gnome Shell, install the package “gnome-shell“,

open a terminal and enter the command gnome-shell –replace&“.

2. More Fast Boot: After using Ubuntu 9.10, I am sure that the system boots extremely fast, takes very very little time.  And I suppose that 10.04 will also lead to the same experience. Boot speed has been a hot topic for a long time now and Ubuntu has come a long way in the last few releases. For Ubuntu 10.04 the target boot speed is 10 seconds! The reference platform for this target is a Dell Mini 9 netbook with a slow CPU and fast SSD that makes it an excellent “middle of the road” machine.  Some people’s machines will be slower, some will be faster.There’s also a plan to improve the speed on the installation process. Currently when installing packages in Ubuntu the download is a separate step from the unpack/configure. Ubuntu will be able to install package faster by doing downloads and installs in parallel. While downloading the cpu and disk are mostly idle. While installing the network is idle. Doing them in parallel is a good way to utilize both systems.
Under the hood Ubuntu 10.04 will also be switching to using a swap file, instead of a separate partition. This will allow on-the-fly resizing.

A graphical OS selector is planned for switching (rebooting) between two or more operative systems. The preferred version of the graphical OS selector will have easily recognizable logos for the installed OSs. This will require a lot of work and might not actually land in 10.04.

3. New Boot Splash :

Ubuntu Splash

4. Some other Features:

Mozilla FireFox: Default search engine has been changed to Yahoo! Default Home Page will use either Google or Yahoo! depending on user setting.

Linux Kernel: It includes the 2.6.32-14.20 kernel based on

The likewise-open package, which provides Active Directory authentication and server support for Linux, has been updated to version 5.4. The package supports upgrades from both the officially supported versions 4.0 (Ubuntu 8.04 LTS) and 4.1 (Ubuntu 9.10), as well as the likewise-open5 packages from universe. Since this upgrade involves a lot of configuration file changes and in-place database upgrades, testing and feedback is appreciated.

SNS-> Go Social: Ubuntu now feature built in integration with Twitter, identi.ca, Facebook, and other social networks with theMeMenu in the panel, which is built upon the Gwibber project, which has a completely new, more reliable backend built on top of desktopcouch. Gwibber now also supports a multi-column view for monitoring multiple feeds simultaneously.

Ubuntu 10.04 Lucid Lynx

Ubuntu 10.04 Lucid Lynx

Ubuntu 10.04 will be out on 29th April, 2010

Want to know about Ubuntu More.


Helping those who need help 😀