0% found this document useful (0 votes)
20 views6 pages

Assignment No 547

The document discusses MySQL-JDBC connectivity and how to perform operations like inner join, outer join, and self-join using JDBC. It explains how to load JDBC drivers, connect to a database, execute statements, handle exceptions, and perform different types of joins.

Uploaded by

kingsourabh1074
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Assignment No 547

The document discusses MySQL-JDBC connectivity and how to perform operations like inner join, outer join, and self-join using JDBC. It explains how to load JDBC drivers, connect to a database, execute statements, handle exceptions, and perform different types of joins.

Uploaded by

kingsourabh1074
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment No.

Develop a client server application using MySQL -JDBC connectivity (operations to be


Performed: Inner join, Outer join and self-join).

THEORY:

MYSQL-JDBC Connectivity:

JDBC Drivers:
When you develop JDBC applications, you need to use JDBC drivers to con-vert queries into
a form that a particular database can interpret.The JDBC driver also retrieves the result of SQL
statements and converts the result into equivalent JDBC API class objects that the Java
Application uses.

The JDBC API classes and interfaces are available in the java.sql and the javax.sql packages.
The classes and interfaces perform a number of tasks, such as establish and close a connection
With a database, send a request to a database, retrieve data from a database and update data in
database .The commonly used classes and interfaces in the JDBC API are:
1. DriverManager Class
2. Driver Interface

3. Connection Interface
4. Statement Interface
5. ResultSet Interface
6. SQLException Class

To query a database and display the result using java applications, you need to used
Following Steps:
1. Loading a Driver :

You can load and register a driver Programatically :


(a) Using the forName() method :

The forName() method is available in the java.lang.class pack-age.The syntax to


load a JDBC driver to access a database is :
Class.forName("driver name");

You can load the JDBC Driver using the following method call:
Class.forName("com.mysql.jdbc.Driver");
(b) Using the registerDriver() method :
You can create an instance of the Driver class to load a JDBC driver.The syntax to declare
an instance of the Driver class is :
Driver d=new <driver name>;

Once you have created the Driver object,call the registerDriver() method to register it with the
DriverManager.You can register JDBC driver using the following method call :
DriverManager.registerDriver(d);
2. Connecting to a Database :

The DriverManager class provides the getConnection() method to cre-ate a Connection


object.
Connection getConnection (String <url>):

DriverManager class provides getConnection() method to create a connection object :

String url = "jdbc:mysql:MyDataSource";


Connection con = DriverManager.getConnection(url);

The syntax for a JDBC URL that is passed as a parameter to the getConnection()
method is :
<protocol >:<subprotocol >:<subname>;

3. Creating and Executing JDBC Statements :

You need to create a statement object to send requests to and retrieve results from a
database. The connection object provides the createS-tatement() method to create a
statement object .

Connection con = DriverManager.getConnection("jdbc:mysql:MyData


Source","NewUser","NewPassword");
Statement stmt = con.createStatement();

You can send SQL statements to a database using the statement ob-ject. The statement
interface contains the following methods to send static SQL statement to a database :
(a) ResultSet excuteQuery(String str) :
Excecute the SQL statement and returns a single object of the type,ResultSet.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(<SQL statement>);
(b) int executeUpdate (String str) :
Executes the SQL statements and returns the number of data rows that are a
selected after processing the SQL statement.
Statement stmt = con.createStatement();
int count = stmt.executeUpdate(<SQL statement>);
(c) Boolean execute (String str ) :
Execute SQL statement and returns a boolean value. Statement stmt =
con.createSatement(); Stmt.execute(<SQL statement>);

Querying a Table :

You can retrieve data from a table using the SELECT statement. The
SELECT statement is excuted using the executeQuery() method and
returns the output in the form of a ResultSet object.

String str =SELECT *FROM authors;


Statement stmt =con.createStatement();
ResultSet rs = stmt.executeQuery(str);

Join Operation :

(a) Inner Join :

The join operations that do not preserve non-matched tuples are


called as Inner Join operations.
Inner join is also known as Equi-Join.

Consider the following Branch and Property relations :

Branch No Branch City


B003 Glasgow
B004 Bristol
B002 London

Property No Property City


PA14 Aberden
PL94 London
PG4 Glasgow

Now,we have to List Branch O ces and properties that are in the
same city.This can be writen as -

SELECT B.*,P.* FROM Branch B,Property P WHERE B.Branch City=P.Property C

Output of the above Query :

Branch No Branch City Property No Property City


B003 Glasgow PG4 Glasgow
B002 London PL94 London
(b) Outer Join :

The join operations that preserve those tuples that would be lost in a
join, by creating tuples in the result containing null values are called
as Outer Join operations.

i. Left Outer Join :

The left outer join preserves tuples only in the relation named
before (to the left of) the left outer join operation.
Now,we have to List Branch O ces and properties that are in the
same city along with any unmatched branches.This can be
writen as -

SELECT B.*,P.* FROM Branch B LEFT JOIN Property P ON


B.Branch City=P.Property City;

Output of the above Query :

Branch No Branch City Property No Property City


B003 Glasgow PG4 Glasgow
B004 Bristol null null
B002 London PL94 London

ii. Right Outer Join :

The right outer join preserves tuples only in the relation named
after (to the right of) the right outer join operation.

Now,we have to List Branch O ces and properties that are in the
same city along with any unmatched property.This can be
writen as -

SELECT B.*,P.* FROM Branch B RIGHT JOIN Property P


ON B.Branch City=P.Property City;

Output of the above Query :


Branch No Branch City Property No Property City
null null PA14 Aberden
B002 London PL94 London
B003 Glasgow PG4 Glasgow
(c) Self Join :
The join operation that joins the table itself are called as Self join.
Consider the following Sta relation :

Sta No Sta Name Mgr Id


01 X 03
02 Y 03
03 Z
04 P 06
05 Q 06
06 R
Now,we have to display sta details along with thier manager Id and
Mgr Name .This can be writen as -

SELECT s1.Sta No,s1.Sta Name ,s1.Mgr Id,s2.Sta Name As


MgrName from Sta s1,Sta s2 WHERE s1.Mgr Id=s2.Sta No;

Output of the above Query :


Sta No Sta Name Mgr Id MgrName
01 X 03 Z
02 Y 03 Z
04 P 06 R
05 Q 06 R
4. Handling SQL Exception :
The java.sql package provides the SQLEx- ception class, which is de-
rived from the java.lang.Exception class. The SQLException is thrown by
varios methods in the JDBC API and enables you to determine the reason
of the errors that occur while connecting to a database. You can catch the
SQLException in a Java application using the try and catch exception
handling block.
ECLIPSE SETUP:

In Eclipse we need to do setup so that JDBC connection can be established.


Following are the steps.
1. Get platform independent JDBC connector mysql-connector-java-5.1.36.jar
2. Go to eclipse, start new java project, right click on project –Build –Add
external jar file and select the above file . Now you get the connection driver
established with mysql.
CONCLUSION: By doing this experiment we have understood the concept of JDBC
connection to Mysql and Joins such as Inner join, left join and right join

You might also like