Using JOINING Operation in JDBC

Whenever we need to select data from two or more tables, we have to perform a join operation.

Using JOINING Operation in JDBC


Whenever we need to select data from two or more tables, we have to perform a join operation. Tables in a database can be related to each other with keys. A primary key is a column with a unique value for each row. Each primary key value must be unique within the table. We will use two related tables Student and testing, to use JOIN. The SQL JOIN clause is used to retrieve data from two or more tables joined by same fields. The most common thing is a primary key from one table matches a foreign key in another table. Consider the SQL JOIN statement below:

SELECT * FROM Student s,testing t where;

There are two types of SQL JOINS ?? INNER and OUTER Joins. By default it takes INNER JOIN. The INNER JOIN will select all rows from both tables as match between the columns. In case we have a students in the Student table, this student will not be listed in the result of our SQL query above.

The join clause binds columns of both the tables to create a single table. Join matches the columns in both tables. There are two types of join statements, inner and outer joins. An inner join returns all rows that result in a match such as the example above.

This join clause has two parts.

A Left outer join returns all rows of the left of the conditional even if there is no right column to match.

A right outer join will display rows on the right side of the conditional that may or may not have a match.

This program is about to join the two tables Student and testing which has the Name and name columns respectively. Which data will match with the tables it will retrieve. Like the Name of Student table and name of testing will match the data and retrieve the all matched data.

import java.sql.*;
  public class JoinPro {
  public static void main(String a[])
 Connection con = null;
 String url = "jdbc:mysql://localhost:3306/";
 String dbName = "vineej";
 String driver = "com.mysql.jdbc.Driver";
 String userName = "vineej";
 String password = "no";
 try {
  con = DriverManager.getConnection(url+dbName,userName,password);
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery("select * 
from Student s,testing t where");
  while( ) {
  String data = rs.getString(1);
  System.out.println( data );
  catch( Exception e ) {

Describe the program
In this program we used how we retreive the data from two or more tables with the JDBC. Student and testing is the name of tables and Name and name are the columns of tables.In the above program see s.Name and, here s and t are the two aliases of tables we have created. They represents the tables at the time of joining.

Coding steps of the program
First in the program we import the java.sql and packages then define the SelectState class.

import java.sql.*;

2.Create a connection with the MySql database.

con = DriverManager.getConnection(url+dbName,userName,password);

3.Create statement and then execute query with the SELECT statement.

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Student s,testing t where");

4.Finally got the result by the getString() method.

while( ) {
String data = rs.getString(1);
System.out.println( data );

5.After all connection should be closed.


6.printStackTrace() method.
The method is used to show error messages. If the connection is not connected then it throws the exception and print the message.

Download Source Code

Share on Google+Share on Google+

Using JOINING Operation in JDBC

Posted on: June 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Using JOINING Operation in JDBC  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image