Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to configure Java DataBase Connectivity

In this article we will talk about the database connectivity of Java platform. We will see how to work with JDBC (Java Database Connectivity).

JDBC (Java Database Connectivity) is Java application programming edge which allows the Java programmers or Java Developer to access database management system from the Java code. This system was developed by JavaSoft which is the subsidiary of Sun Microsystems.

Definition of the JDBC:

Java Database Connectivity in small way called as JDBC. JDBC is a java API (Application Programming Interface) that enables the java programs to execute SQL statements. This way most people trust that JDBC stands for Java Data Base Connectivity. As a point of awareness, JDBC is a trademark name and is not an acronym. JDBC can be defined as an application programming edge that explains how a java programmer can access the database in tabular format from Java code using a set of standard interfaces and classes written in the Java programming language. The Java Database Connectivity which has been developed under the Java Community Process allows multiple implementations to exist and be used equally by all applications. JDBC provides methods for querying and updating the data in Relational Database Management system such as SQL, Oracle etc.

Java application programming interface provides a mechanism for dynamically loading the precise, Java packages and drivers and registering them with the JDBC Driver Manager that is used as a connection factory for creating JDBC connections which supports creating and executing statements such as SQL INSERT, UPDATE and DELETE. Driver Manager is the backbone of the JDBC architecture.

In general all Relational Database Management System (RDMBS) supports SQL and we all know that Java is platform independent, so JDBC makes it possible to write a single database application that can run on different platforms and interact with different Database Management Systems.

Java Database Connectivity is similar to Open Database Connectivity (ODBC) which is used for accessing and managing database, but the difference is that JDBC is planned precisely for Java programs or Java Developer whereas ODBC is not depended upon any language. So it is very much useful for us for developing our programming model in case of database process. Generally the JDBC helps the programmers or the developer to write java applications that manage these three programming activities, these activities are given below.

  • JDBC helps us to connect to a data source basically it is called database.
  • JDBC helps us in sending queries and updating statements to the database.
  • Reprocessing and processing the results received from the database in terms of answering to our query.

Advantages of Java Data Base Connectivity:

Following are the main advantages of JDBC connectivity.

  • JDBC API permits connection between Java program and DB.
  • It also provides cross-vendor connectivity.
  • The standard classes and interfaces are available to access the database following standard way.
  • The JDBC drivers are used by JVM to translate JDBC calls into vendor specific database calls.
  • Most web applications and stand alone applications use database to store data in a persistent way.
  • JBDC allows similar technique for accessing databases from standalone and web applications.

About JDBC Drivers:

JDBC drivers can be classified in the following way.

  • JDBC-ODBC bridge.
  • Part Java, Part Native Driver.
  • Intermediate Access Server.
  • Pure Java Drivers.

Overview of JDBC-ODBC Bridge:

Let us also check some details about JDBC-ODBC Bridge.

  • ODBC (Open DataBase Connectivity) is a set of APIs (Application Programming Interface) for database access.
  • Originally C interfaces.
  • The standard JDK includes classes for the JDBC-ODBC bridge (sun.jdbc.odbc.JdbcOdbcDriver)
  • There is no need for additional installation, apart from having to configure the ODBC driver by creating data source names.

Step by step procedure in the creation of DSN (Data Source Name):

At first we go start menu -> Control Panel -> ODBC (Open database connectivity) option. This part is responsible for generate the DSN (Data Source Name) connection process.

Showing DSN creation

Figure 1: Showing DSN creation

After that we go to Create new data source option -> select Microsoft Access Driver (*.mdb) [Basically we can connect different database file as per our choice like as Oracle, Excel, etc. But here we connect it because our programming database is present in MS Access]

Create new data source

Figure 2: Create new data source

After the crossing the above step we just select or locate our database and press OK button

Select database

Figure 3: Select database

Then give the DSN name which is use in the program. Here we use the database name like “srimani” and press OK.

Give DSN name

Figure 4: Give DSN name

We find out that at User DSN tab we get “srimani” name is display with prominently.

Showing name

Figure 5: Showing name

Listing 1: Sample showing connecting

import java.io.*;
import java.sql.*;
public class srijdbc
{
	String url;
	public static void main(String args[])
	{
		Statement st; //create statement
		Connection conn;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException e)
		{
			System.err.print("ClassNotFoundException:  ");
			System.err.println(e.getMessage());
		}
		try
		{
			conn = DriverManager.getConnection("jdbc:odbc:srimani");//srimani is name of ODBC data source
			st = conn.createStatement( );
			ResultSet rs=st.executeQuery("select * from marks");
			while(rs.next())
			{
			System.out.print(rs.getInt("roll")+"  ");
			System.out.print(rs.getString("name")+"  ");
			System.out.print(rs.getInt("marks"));
			System.out.println();
			}
			st.close();
			conn.close();
		}

After the compilation the program we find out the following output. The data are retrieving from the database.

Displaying output

Figure 6: Displaying output

Listing 2: This is another example through the JDBC program, which display next record

import javax.swing. *;
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class QuaryApp extends JFrame implements ActionListener
{
JTextField pid1;
JTextField pname1;
JButton next1;
JButton exit1;
JPanel p1;
//These variable are declared as ststic beacuse they have to be accessed in a static method.
static ResultSet res;
static Connection con;
static Statement stat;
//The constructor of search class 

public search()
{
	                 super("The Searching Procedure System");
	setLayout(new GridLayout(3,2));
                                                         pid1 =new JTextField(20);
                                                         pname1 =new JTextField(20);
                                                         next1=new JButton("Next");
                                                        exit1=new JButton("Exit");
                                                        p1=new JPanel();
	add(new JLabel("Employee ID:"));
		add(pid1);
		add(new JLabel("Employee Name:"));
		add(pname1);
		add(p1);
		p.add(next1);
		p.add(exit1);
		next.addActionListener(this);
		exit.addActionListener(this);
		pack();		
		setVisible(true);
	}
	//The main method creates an object of the class and display the first record.

	public static void main(String a[])
	{
		search obj=new search (); //initialized  object through obj variable
		try
		{
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    con=DriverManager.getConnection("jdbc:odbc:emp","srimani");
                               stat=con.createStatement();
	res=stat.executeQuery("select empno,ename from emp");
			res.next();
		}
		catch(Exception e)
		{
			System.out.println("Error "+e);
		}
		obj.searchRecord(res);
	}
	//Event handling routine for the next button
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource()==next)
		{
			try
			{
				res.next();
			}
			catch(Exception e)
			{ }
			showRecord(res);
		}
		if(event.getSource()==exit)
		{
			System.exit(0);	
			//con.close();
		}
	}
	//The showRecord method display a record
	public void searchRecord(ResultSet result)
	{
		try
		{
			pid.setText(result.getString(1));
			pname.setText(result.getString(2));
		}
		catch(Exception e)
		{
		}
	}
}

When we are compiling our program if the compilation found no error then we find out that a Graphical User Interface window is now open and we easily access it with very successively way.

Displaying input window

Figure 7: Displaying input window

Conclusion:

JDBC is the system that can handle all types of database programming like INSERT,UPDATE,DELETE. These types of programming are totally related with the dynamic programming concept which most of the website are based on. This system is highly secured and prominent when we are working using JSP (Java Server Page). The page is related with JDBC programming concept. Overall we can say that this JDBC programming model is totally very useful for us at all levels of java database programming.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login