Natural Join / joining two tables

Posted in : Core Java Posted on : October 18, 2010 at 6:18 PM Comments : [ 0 ]
This section contains the detail about the Natural Join / joining two tables in java.

Natural Join / joining two tables

A join provide the facility to merge two tables according to common field and Create a new virtual field. We will use the following query :

SELECT * FROM employee NATURAL JOIN emp_sal

Here, "NATURAL JOIN"  join the two table according to common field and store it in "Statement " class object.

The two tables before join --

"employee" table

"emp_sal" table

import java.sql.*;

public class NatJoin{
public static void main(String[] args) {
System.out.println("Table after Natural Join");
Connection con = null;
con = DriverManager.getConnection("jdbc:mysql://","root","root");
Statement st = con.createStatement();
//Join two tables
ResultSet res = st.executeQuery("SELECT *FROM "+"employee"+" NATURAL JOIN "+"emp_sal");

System.out.println("Emp_code" + "\t" + "Emp_name" + "\t" + "Emp_salary"+"\t"+"Emp_designation");
int code = res.getInt("Emp_code");
String name = res.getString("Emp_name");
String sal = res.getString("Emp_salary");
String post = res.getString("Emp_designation");

System.out.println(code + "\t\t" + name + "\t\t" + sal+"\t\t"+post);
catch (SQLException s){
	System.out.println("Error in excution of SQL statement");
catch (Exception e){

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

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