Natural Left Join of tables using DBCP


 

Natural Left Join of tables using DBCP

This tutorial demonstrate natural left join of the tables using DBCP.

This tutorial demonstrate natural left join of the tables using DBCP.

Code:

import java.sql.*;
import org.apache.commons.dbcp.BasicDataSource;

public class NaturalLeftJoin {
  public static void main(String[] args) {
    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("com.mysql.jdbc.Driver");
    bds.setUrl("jdbc:mysql://localhost:3306/mydb");
    bds.setUsername("root");
    bds.setPassword("");
    try {
      Connection con = bds.getConnection();
      try {
        Statement st = con.createStatement();
        ResultSet res = st.executeQuery("SELECT *FROM " "employee"
        " NATURAL LEFT JOIN " "salary");
        System.out.println("Emp_name" "\t" "Emp_sal");
        while (res.next()) {
          String name = res.getString("emp_name");
          int sal1 = res.getInt("sal");
          System.out.println(name + "\t\t" + sal1);
        }
      catch (SQLException s) {
        System.out.println("SQL statement is not executed!");
      }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Before running this program you need to make two tables. Below shown two tables and record stored in it.

Salary

Employee

Directory structure of files:

In the lib folder place all required jar files ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar, j2ee.jar and mysql-connector-java-5.1.7-bin.jar

In the pathset.bat write following statement

set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

Output:

Ads