UNIT-V Notes Advance Java
UNIT-V Notes Advance Java
UNIT-V Notes Advance Java
• JDBC Architecture:-
1) Two-Tier Application Architecture.
• Diagram:-
• JDBC Driver:-
JDBC Driver is a software component that enables fava application to interact
with the database. There are 4 types of JDBC drivers:
Diagram:-
Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends
that you use JDBC drivers provided by the vendor of your database instead of
the JDBC-ODBC Bridge.
Advantages:
1)easy to use.
Disadvantages:
1)Performance degraded because JDBC method call is converted into the
ODBC function calls.
2) Native-API driver:-
The Native API driver uses the client-side libraries of the database. The driver
converts JDBC method calls into native calls of the database API. It is not written
entirely in java.
Diagram:-
Advantage:
1) performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
1) The Native driver needs to be installed on the each client machine.
2) The Vendor client library needs to be installed on client machine.
Diagram:-
Advantage:
1) No client side library is required because of application server that can
perform many tasks like auditing, load balancing, logging etc.
Disadvantages:
1) Network is required on client
2) Requires database-specific coding to be done in the middle tier.
3) Maintenance of Network Protocol driver becomes costly because it requires
database specific coding to be done in the middle tier.
4) Thin driver:-
The thin driver converts JDBC calls directly into the vendor-specific database
protocol. That is why it is known as thin driver. It is fully written in Java
language.
Diagram:-
Advantage:
1) Better performance than all other drivers.
2) No software is required at client side or server side.
Disadvantage:
Drivers depend on the Database.
2)Create connection
3)Create statement
4)Execute queries
5)Close connection
Class.forName("oracle.jdbc.driver.OracleDriver");
***************************************************************
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521:xe","syst em","password");
***************************************************************
Statement stmt=con.createStatement();
***************************************************************
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
Con.close();
• Code:-
import java.sql.*;
class JDBCDemo
{
public static void main(String args[])throws SQLException
{
try
{
//Register Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Example to establish connection with the Oracle database
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521:xe","system","password");
//Create Statement Object
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select *from Student");
while(rs.next())
{
System.out.println(re.getInt(1)+" "+rs.getString(2));
}
//Close The Connection
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
***************************************************************
• ***Class:-
-> static Class forname(String className)
• ***DriverManager class:-
-> static Connection getConnection(String URL)
• ***Connection interface:
-> Statement createStatement();
• ***Statement Interface:-
-> void close();
• ***PreparedStatement Interface:
-> boolean execute()
• ***ResultSet Interface:
-> boolean next()
• Code:-
//Database Connection.
import java.sql.*;
class CreateTableDemo
try
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/X
E","system","system");
}catch(Exception e)
System.out.println(e);
• Output:-
****************************************************************
• Code 1):-
//Create Table Program.
import java.sql.*;
class CreateTableDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
Statement stmt=con.createStatement();
stmt.execute("create table vjtech(rollno number(5),name
varchar(10),marks number(3,2))");
System.out.println("Table Created Successfully!!!");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:-
2)
import java.sql.*;
class InsertRowsDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
Statement stmt=con.createStatement();
int x=stmt.executeUpdate("insert into vjtech values(3030,'James',9.9)");
System.out.println(" No of Rows Inserted into table:"+x);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
****************************************************************
3)
import java.sql.*;
class UpdateRowsDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
Statement stmt=con.createStatement();
int x=stmt.executeUpdate("update vjtech set marks=5.5 where
rollno=2020");
System.out.println(" No of Rows Updated from table:"+x);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
****************************************************************
4)
import java.sql.*;
class DeleteRowsDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
Statement stmt=con.createStatement();
int x=stmt.executeUpdate("delete from vjtech where rollno=3030");
System.out.println(" No of Rows Deleted from table:"+x);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
****************************************************************
5)
import java.sql.*;
class SelectRowsDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeUpdate("select *from vjtech");
System.out.println("\n****************************
*****************");
System.out.println("\n***************VJTech Academy
*****************");
System.out.println("\n**************************
********************");
System.out.println("\nRollNo\tName\tmarks");
System.out.println("\n======================");
while(re.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+
"\t"+rs.getFloat(3));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
****************************************************************
6)
import java.sql.*;
import java.util.*;
class PreparedStatementInsertRowsDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
PreparedStatement pstmt=con.prepareStatement("insert into vjtech
values(?,?,?)");
Scanner sc=new Scanner(System.in);
pstmt.setInt(1,rn);
pstmt.setString(2,nm);
pstmt.setFloat(3,mrks);
int x=pstmt.executeUpdate();
catch(Exception e)
{
System.out.println(e);
}
}
}
****************************************************************
7)
import java.sql.*;
import java.util.*;
class PreparedstmtDeleteRowsDemo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully!!!");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
System.out.println("Connection Established Successfully!!!");
PreparedStatement pstmt=con.PrepareStatement("delete from vjtech
where rollno=?");
Scanner sc=new Scanner(System.in);
System.out.println("Enter Roll No for Deletion:");
int rn=sc.nextInt();
pstmt.setInt(1,rn);
int x=pstmt.executeUPdate();
catch(Exception e)
{
System.out.println(e);
}
}
}
****************************************************************
• ***ResultSetMetaData
-> Data about data is known as metadata.
ResultSetMetaData rsmd=rs.getMedaData();
5) boolean isNullable();
• Code:-
import java.sql.*;
class ResultSetMetaDataDemo
try
{ Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost:1521/XE","system","system");
Statement stmt=con.createStatement();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Number
of Coloums="+rsmd.getColumnCount());
System.out.println("Name of
1st Coloum="+rsmd.getColumnName());
1st Coloum="+rsmd.getColumnTypeName(1));
System.out.println("Name of table="+rsmd.getTableName(2));
con.close();
catch(Exception e)
System.out.println(e);
We can have business logic on the database by the use of stored procedures
and functions that will make the performance better because these are
precompiled.
Suppose you need the get the age of the employee based on the date of birth,
you may create a function that receives date as the input and returns age of
the employee as the output.
The differences between stored procedures and functions are given below:
To call the stored procedure, you need to create it in the database. Here, we
are assuming that stored procedure looks like this.
In this example, we are going to call the stored procedure INSERTR that
receives id and name as the parameter and inserts it into the table user420.
Note that you need to create the user420 table as well to run this application.
Now check the table in the database, value is inserted in the user420 table.
• Code 1):-
import java.sql.*;
import java.util.*;
class CallableStmtStoredProcedureDemo
try
Class.forName("oracle.jdbc.driver.OracleDriver");
Connectioncon=DriverManager.getConnection("jdbc:oracle:
thin:@localhost:1521/XE","system","system");
System.out.println("Connection established
successfully!!!");
CallableStatement Cstmt=con.prepareCall("{call
InsertRows(?,?,?)}");
int rn=sc.nextInt();
String nm=sc.next();
float mrks=sc.nextFloat();
Cstmt.setInt(1,rn);
Cstmt.setString(2,nm);
Cstmt.setFloat(3,mrks);
int x=Cstmt.executeUpdate();
catch(Exception e)
System.out.println(e);
• Code 2):-
import java.sql.*;
import java.util.*;
class CallableStmtFunctionsDemo
try
Class.forName("oracle.jdbc.driver.OracleDriver");
Connectioncon=DriverManager.getConnection("jdbc:oracle:
thin:@localhost:1521/XE","system","system");
System.out.println("Connection established
successfully!!!");
CallableStatement Cstmt=con.prepareCall("{?=call
Sum1(?,?)}");
Cstmt.setInt(2,50);
Cstmt.setInt(3,80);
Cstmt.registerOutParameter(1,Types.INTEGER);
Cstmt.execute();
System.out.println("Addition of two
numbers="+Cstmt.getInt(1));
catch(Exception e)
System.out.println(e);
VJTech Academy…