Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 56

dt : 27/9/2021(Day-1)

Java:

part-1 : CoreJava

(i)Writing programs using Class-Object concept

(ii)Variables

(iii)Methods

part-2 : AdvJava

=>AdvJava provides the following three technologies:

1.JDBC

2.Servlet

3.JSP

=>Using these three technologies we can construct WebApplications.

faq:

define Application?

=>The set-of-programs collected together to perform defined action is

known as Application.

Diagram:
faq:

define WebApplication?

=>The application which is executing in Internet Environment or Web

Environment is known as WebApplication or Internet Application.

Structure of WebApplication:

JDBC - JDBC Stands for 'Java DataBase Connectivity' and which is used to

interact with DataBase.

Servlet - Servlet is a 'Server program' and which accept the request from

User/Client through WebBrowser.

JSP - JSP Stands for 'Java Server Page' and which is response from

WebApplication.

--------------------------------------------------------------------

JDBC - Unit-1 (15-20 Sessions)

Servlet - Unit-2(25-30 Sessions)


JSP - Unit-3 (10 - 12 Sessions)

----------------------------------------------------------------------

Dt : 28/9/2021

JDBC(Unit-1)

=>Types of Storages related to Java Application Development:

1.Field Storage

2.Object Storage

3.File Storage

4.DataBase Storage

1.Field Storage:

=>The memory generated to hold single data value is known as Field

Storage.

Note:

=>Field Storages are generated when we use Primitive DataType variables.

*imp

2.Object Storage:

=>The memory generated to hold group members is known as Object

Storage.

diagram:
Note:

=>These Object Storages will be generated when we use NonPrimitive

DataTypes or Referential DataTypes.

---------------------------------------------------------------------

Note:

=>The Field Storages and Object Storages,which are generated part of

application are temporary storages because these Storages will be destroyed

automatically when JVM ShutDowns.

=>when we want to have permanent storage for WebApplication,then we use

any one of the following:

=>File Storage

=>DB Storage

------------------------------------------------------------------------

Dt : 29/9/2021

3.File Storage:

=>The smallest permanent storage of Computer System which is managed by

the OperatingSystem is known as File Storage.

Note:
=>when we want to establish communication b/w JavaApp and File Storage,

the JavaApp must be constructed using the classes and Interfaces available

from 'java.io' package.

(java.io package is also known as 'File API')

Diagram:

faq:

define API?

=>API stands for 'Application Programming Interface' and which provide

the related 'classes and Interfaces' used in application development.

=>Each package from JavaLib can be called as API.

-------------------------------------------------------

*imp

DisAdvantage of FileStorage:
1.Data Redundancy

2.Data Inconsistency

3.Difficuilty in Accessing Data

4.Limited Data Sharing

5.Integrity Problems

6.Atomicity Problems

7.Concurrent Access Anomalies

8.Security problems

1.Data Redundancy:

=>It is possible that the same information may be duplicated in diff

files and leads to a data redundancy.(memory wastage)

2.Data Inconsistency:

=>Because of data redundancy,it is possible that data may not be in

consistant state.

3.Difficuilty in Accessing Data:

=>Accessing data is not convenient and efficient in file processing

System.

4.Limited Data Sharing:

=>In file-based-storage the data is available in Scattered form and

difficult to share the data.


5.Integrity Problems:

=>Integrity is not possible in File-based-storage becuase the data is

available in scattered form.

6.Atomicity Problems:

=>Any operation on database must be atomic,which means it must happen

entirely or not at all.

=>which is not possible in File-based-Storage.

7.Concurrent Access Anomalies:

=>Multiple users are allowed to access data simultaneously and this is

for better performance and faster response.

=>which is not possible in File-based-storage.

8.Security problems:

=>Security is very high in DB and which is not possible in

File-based-storage.

=====================================================================================

Dt : 30/9/2021

*imp

4.DataBase Storage:

=>The largest permanent storage which is installed into Computer

Environment from externally is known DataBase Storage.

Note:
=>when we want to establish communication b/w JavaApp and DataBase product

then the JavaApp must be constructed using the classes and Interfaces

available from 'java.sql' package,and it has to take the support JDBC

driver.

Diagram:

--------------------------------------------------------------------

define driver?

=>The small s/w program used by operating system to control resources of

Computer System is known as 'driver'.

ex:

Audio driver

Video driver

N/W driver
------------------------------------------------------------------

define JDBC driver?

=>The driver which is used to establish communication to DataBase product

is known as JDBC driver.

(Java DataBase Connectivity driver)

=>JDBC Drivers are categorized into the following:

1.JDBC-ODBC bridge driver(Type-1)

2.Native API driver(Type-2)

3.Network Protocol driver(Type-3)

4.Thin driver(Type-4)

Dt : 1/10/2021

1.JDBC-ODBC bridge driver(Type-1):

=>In Type-1 driver,JDBC Call is converted into ODBC Call and this ODBC

call is converted into Database Specific call for Connection.

DisAdvantage:

=>Type-1 driver having more number of conversions and consumes more

execution time and degrades the performance.

Note:

=>Type-1 driver is not available from Java8 version onwards.

Diagram:
define ODBC?

=>ODBC Stands for 'Open DataBase Connectivity' and which is used to

Convert Java Calls(JDBC calls) into Native ODBC calls.

=>ODBC is PlatForm dependent driver,becuase which internally having c/c++

code.

2.Native API driver(Type-2):

=>In Type-2 driver,the Client Machine is installed with DataBase related

Libraries these Libraries will Support Type-2 driver to convert JDBC calls

into DataBase Specific calls for connection.

DisAdvantage:

=>In Type-2 driver the Application will become DataBase dependent

application.

Diagram:
3.Network Protocol driver(Type-3):

=>In Type-3 driver,we make a call to MiddleWare-Server and this MiddleWare

Server will establish connection to DB product.

DisAdvantage:

=>In Type-3 driver,more number of N/W components are participated in

execution process and increases the execution time and degrades the

performance of an application.

Diagram:
*imp

4.Thin driver(Type-4):

=>In Type-4 driver,the JDBC calls are converted into DataBase Specific

calls directly using DataBase protocol and the connection is established.

diagram:

Note:

=>Thin driver is pure Java Driver and High performance driver.

=====================================================================

Dt : 4/10/2021

*imp

Making Computer System Environment ready to execute JDBC Applications:

step-1 : DownLoad and Install Oracle product.

Note:

Type 'Step-by-step guide for installation of oracle 11g Express Edition'

in SearchEngine
step-2 : perform Login process using 'SQL Command Line'

start->search->type 'run sql Command line' and press 'enter'

SQL> Connect(press Enter)

Enter user-name:system(press enter)

Enter password :manager(press enter)

connected

SQL>

step-3 : Create table with name 'Product38'(pcode,pname,pprice,pqty)

Creating Table:

create table Product38(pcode varchar2(10),pname varchar2(15),

pprice number(10,2),pqty number(10),primary key(pcode));

Inserting Data into Table:

insert into Product38 values('A121','Mouse',123.45,12);

View data from the Table:

select * from Product38;


SQL> select * from Product38;

PCODE PNAME PPRICE PQTY

---------- --------------- ---------- ----------

A121 Mouse 123.45 12

A122 KB 200.45 10

A123 FDD 187.45 9

SQL> commit;

Commit complete.

SQL>

================================================================

Assignment1:

Create table with name Book38(bcode,bname,bauthor,pprice,bqty)

Primary key : bcode

insert min 5 book details

Assignment2:

Create table with name Customer38(custId,custname,accno,balance,acctype)

primary key : custId

========================================================================

Dt : 5/10/2021
*imp

DataBase JAR files:

=>JAR stands for 'Java Archieve' and which is compressed format of

class files.

Oracle - ojdbc14.jar,ojdbc6.jar, ojdbc7.jar, ojdbc8.jar,ojdbc10.jar

Oracle10 - ojdbc14.jar

Oracle11 - ojdbc6.jar

oracle12 - ojdbc7.jar,ojdbc8.jar

other - UPC.jar(Universal Connection pool)

(ojdbc10.java)

MySQL - mysql-connector-java-VERSION.jar

SQL Server - sqljdbc41.jar, sqljdbc42.jar

PostgreSQL - postgresql-VERSION.jar

Apache Derby - derby.jar, derbyclient.jar

SQLite - sqlite-jdbc-VERSION.jar

Microsoft Access - ucanaccess-VERSION.jar

Note:

=>DB Jar file is available at lib of Oracle product:

C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib
=>Create one User defined folder on desktop and copy the DB Jar file

-------------------------------------------------------------------

*imp

DataBase Port No and Service Name:

=>Every database will have its own PortNo and ServiceName,to know the

PortNo and ServiceName of database open 'tnsnames.ora' file available at:

C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN

Port No : 1521

service name : XE

-------------------------------------------------------------

*imp

DataBase API:

=>'java.sql' is known as DataBase API or JDBC API,which provides related

classes and Interfaces used to develop JDBC Application.

=>'java.sql.Connection' is an interface and which is root of JDBC API.

faq:

define 'Connection'?

=>'Connection' is an interface from java.sql package and which is root

of JDBC API.

=>The following are some important methods of 'Connection' interface:

(a)createStatement()

(b)prepareStatement()
(c)prepareCall()

(d)setAutoCommit()

(e)getAutoCommit()

(f)commit()

(g)rollback()

(h)setSavepoint()

(i)releaseSavepoint()

(j)close()

Dt : 6/10/2021

Note:

=>we use getConnection() method to create the implementation object of

'Connection' interface.

=>This getConnection() method is available from 'java.sql.DriverManager'

class.

Method Signature of getConnection() method:

public static java.sql.Connection getConnection(java.lang.String,

java.lang.String,java.lang.String)throws java.sql.SQLException;

syntax:

Connection con = DriverManager.getConnection("url","uname","pword");

url => jdbc:oracle:thin:@localhost:1521:xe


uname : system

pword : manager

ex:

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
-------------------------------------------------------------

Dt : 7/10/2021

*imp

Types of Statements in JDBC:

=>Statements in JDBC are categorized into three types:

1.Statement

2.PreparedStatement

3.CallableStatement

1.Statement:

=>'Statement' is an interface from java.sql package and which is used to

execute Normal queries like Create,Insert,Retrieve(select),update and delete.

=>The following are some important methods from 'Statement':

(a)executeQuery()

(b)executeUpdate()
(a)executeQuery():

=>executeQuery() method is used to execute 'select' queries.

Method Signature:

public abstract java.sql.ResultSet executeQuery(java.lang.String) throws

java.sql.SQLException;

(b)executeUpdate():

=>executeUpdate() method is used to execute Non-Select queries like

Create,Insert,Update and Delete.

Method Signature:

public abstract int executeUpdate(java.lang.String)throws

java.sql.SQLException;

-----------------------------------------------------------------
=>we use createStatement() method from 'Connection' interface to create the

implementation object of 'Statement'.

Method Signature of createStatement():

public abstract java.sql.Statement createStatement() throws

java.sql.SQLException;

Syntax:

Statement stm = con.createStatement();

ResultSet rs = stm.executeQuery("select-query");

int k = stm.executeUpdate("Non-Select-Query");

====================================================================

*imp

Constructing JDBC Application Using IDE Eclipse:

step-1 :Open IDE Eclipse,while opening name the WorkSpace and click 'launch'

step-2 : Create Java Project

Click on File->new->Project->Java->select 'Java Project' and click next->

name the project and click 'finish'

step-3 : Add DB-Jar to Java Project

RightClick on Project->Build path->Configure Build path->Libraries->

select 'Classpath' and click 'Add External Jars'->Browse and select


DB-Jar file->Open->Apply-> Apply and Close.

step-4 : Create package within the project

RightClick on 'src'->new->package,name the package and click 'finish'

step-5 : Create Class within the package

RightClick on package->new->Class,name the class and click 'finish'

step-6 : Construct the following code to display all records from

DB-Table 'Product38'

package test;

import java.sql.*;

public class DBCon1 {

public static void main(String[] args) {

try {

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

Statement stm = con.createStatement();

ResultSet rs = stm.executeQuery

("select * from Product38");

while(rs.next())

System.out.println(rs.getString(1)+"\t"+

rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+
rs.getInt(4));

}//end of loop

}catch(Exception e) {

System.out.println(e.getMessage());

step-7 : Execute the program

Click Run->Run

================================================================

Dt : 8/10/2021

PCODE PNAME PPRICE PQTY

---------- --------------- ---------- ----------

A121 Mouse 123.45 12

A122 KB 200.45 10

A123 FDD 187.45 9

A124 DR 187.45 9

faq:

define ResultSet?

=>ResultSet is an interface from java.sql package and which holds the

result of 'select' query.

=>executeQuery() method will create the implementation object of


'ResultSet' interface and the object will hold select-query result.

=>The following are some important methods from 'ResultSet':

(i)getString()

(ii)getInt()

(iii)getFloat()

(iv)getLong()

(v)next()

...

=======================================================================

Execution behaviour of next() method:

=>next() method move the cursor row-by-row on ResultSet object

=>return type of next() method is 'boolean'

=>next() method returns 'true' when record available,else returns 'false'

Diagram:

-----------------------------------------------------------------

Assignment1:

Create table with name Book38(bcode,bname,bauthor,pprice,bqty)


Primary key : bcode

insert min 5 book details from SQL-Command Line

Construct JDBC Application to display all book details.

Assignment2:

Create table with name Customer38(accno,custId,custname,balance,acctype)

primary key : accno

insert min 5 Customer details from SQL-Command Line.

Construct JDBC Application to display all Customer details

--------------------------------------------------------------

JDBC-Application2:

DB Table : Emp38(eid,ename,desg,bSal)

primary key : eid

create table Emp38(eid varchar2(10),ename varchar2(15),desg varchar2(10),

bsal number(10),primary key(eid));

Construct Application to read data from Console(CommandPrompt) and insert

into DB Table.

package test;
import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import java.util.Scanner;

public class DBCon2 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

System.out.println("Enter the EmpId:");

String eId = s.nextLine();

System.out.println("Enter the EmpName:");

String eName = s.nextLine();

System.out.println("Enter the EmpDesg:");

String eDesg = s.nextLine();

System.out.println("Enter the EmpBSal:");

int bSal = s.nextInt();

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

Statement stm = con.createStatement();

int k = stm.executeUpdate

("insert into Emp38 values('"+eId+"','"+eName+"','"+eDesg+"',"+bSal+")");

if(k>0) {

System.out.println("Employee details inserted Successfully");


}

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

o/p:

Enter the EmpId:

A121

Enter the EmpName:

Alex

Enter the EmpDesg:

SE

Enter the EmpBSal:

12000

Employee details inserted Successfully

=========================================================

Assignment3 :

JDBC application to read data from Console and insert into Book38 table

Assignment4 :

JDBC application to read data from Console and insert into Customer38

table
-------------------------------------------------------------

Assignment5:

DB Table : Student38(rollNo,stuname,branch,address,mailid,phno)

primary key : rollNo

(i)JDBC Application to read data from Console and insert into Student38

(ii)JDBC Application to display all Student details

======================================================================

Dt : 11/10/2021

*imp

2.PreparedStatement:

=>PreparedStatement is an interface from java.sql package and which is

also used to execute normal queries with IN Parameters.(which means

parameterized queries)

=>we use prepareStatement() method from 'Connection' interface to create

the implementation object of 'PreparedStatement' interface.

Method Signature of prepareStatement():

public abstract java.sql.PreparedStatement prepareStatement

(java.lang.String) throws java.sql.SQLException;

syntax:

PreparedStatement ps = con.prepareStatement("query-Structure");
=>The following are two important methods of PreparedStatement interface:

(i)executeQuery()

(ii)executeUpdate()

(i)executeQuery():

=>executeQuery() method is used to execute select Queries.

Method Signature of executeQuery():

public abstract java.sql.ResultSet executeQuery()

throws java.sql.SQLException;

syntax:

ResultSet rs = ps.executeQuery();

(ii)executeUpdate():

=>executeUpdate() method is used to execute Non-Select queries.

Method Signature of executeUpdate():

public abstract int executeUpdate() throws java.sql.SQLException;

syntax:

int k = ps.executeUpdate();

--------------------------------------------------------------

Assignment5:(Solution with PreparedStatement)

DB Table : Student38(rollNo,stuname,branch,address,mailid,phno)

primary key : rollNo


create table Student38(rno varchar2(15),sname varchar2(15),

branch varchar2(10),address varchar2(15),mailid varchar2(25),

phno number(15),primary key(rno));

(i)JDBC Application to read data from Console and insert into Student38

package test;

import java.sql.*;

import java.util.*;

public class DBCon3 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

System.out.println("Enter the RollNo:");

String rNo = s.nextLine();

System.out.println("Enter the StuName:");

String sName = s.nextLine();

System.out.println("Enter the Branch:");

String br = s.nextLine();

System.out.println("Enter the Address:");

String addr = s.nextLine();

System.out.println("Enter the MailId:");

String mId = s.nextLine();


System.out.println("Enter the PhoneNo:");

long phNo = s.nextLong();

PreparedStatement ps = con.prepareStatement

("insert into Student38 values(?,?,?,?,?,?)");

//Query-Structure is Compiled

ps.setString(1,rNo);

ps.setString(2,sName);

ps.setString(3,br);

ps.setString(4, addr);

ps.setString(5,mId);

ps.setLong(6, phNo);

int k = ps.executeUpdate();//Query executed with values

if(k>0) {

System.out.println("Details Inserted Successfully");

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

o/p:
Enter the RollNo:

1234

Enter the StuName:

Raj

Enter the Branch:

ECE

Enter the Address:

SRN

Enter the MailId:

r@gmail.com

Enter the PhoneNo:

9898981234

Details Inserted Successfully

---------------------------------------------------------------

Assignment:(Using PreparedStatement)

DB table : UserReg38

(uname,pword,fname,lname,address,mailId,phno)

primary key : uname and pword


Construct JDBC Application to read user details from Console and insert

into DB table

-----------------------------------------------------------------------

Dt : 12/10/2021

Assignment:(Solution)

create table UserReg38(uname varchar2(15),pword varchar2(15),

fname varchar2(15),lname varchar2(15),address varchar2(15),

mailid varchar2(25),phno number(15),primary key(uname,pword));

Construct JDBC Application to read user details from Console and insert

into DB table(UserReg38)

ex program:

package test;

import java.sql.*;

import java.util.*;

public class DBCon4 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection


("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

System.out.println("Enter the UserName:");

String uName = s.nextLine();

System.out.println("Enter the PassWord:");

String pWord = s.nextLine();

System.out.println("Enter the FirstName:");

String fName = s.nextLine();

System.out.println("Enter the LastName:");

String lName = s.nextLine();

System.out.println("Enter the Address:");

String addr = s.nextLine();

System.out.println("Enter the MailId:");

String mId = s.nextLine();

System.out.println("Enter the PhoneNo:");

long phNo = s.nextLong();

PreparedStatement ps = con.prepareStatement

("insert into UserReg38 values(?,?,?,?,?,?,?)");

//Query Compilation process

ps.setString(1,uName);

ps.setString(2,pWord);

ps.setString(3,fName);

ps.setString(4,lName);

ps.setString(5,addr);
ps.setString(6,mId);

ps.setLong(7,phNo);

int k = ps.executeUpdate();//query executed with values

if(k>0) {

System.out.println("User registered Successfully");

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

o/p:

Enter the UserName:

nit.v

Enter the PassWord:

mzu672

Enter the FirstName:

Venkat

Enter the LastName:

Enter the Address:

SR Nagar

Enter the MailId:


v@gmail.com

Enter the PhoneNo:

9898981234

User registered Successfully

------------------------------------------------------------

Ex : JDBC Application to display Student details based on rollNo

package test;

import java.util.*;

import java.sql.*;

public class DBCon5 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

System.out.println("Enter the RollNo:");

String rollNo = s.nextLine();

PreparedStatement ps = con.prepareStatement

("select * from Student38 where rno=?");//Compilation

ps.setString(1,rollNo);

ResultSet rs = ps.executeQuery();//Execution

if(rs.next())
{

System.out.println(rs.getString(1)+"\t"+

rs.getString(2)+"\t"+rs.getString(3)+"\t"+

rs.getString(4)+"\t"+rs.getString(5)+"\t"+

rs.getLong(6));

else

System.out.println("Invalid RollNo...");

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

o/p:

Enter the RollNo:

1234

1234 Raj ECE SRN r@gmail.com 9898981234

--------------------------------------------------------

Assignment1:

JDBC Application to display user details based on uname and pword.


Assignment2:

JDBC Application to display Book details based on bCode.

Assignment3:

JDBC Application to display Customer details based on custId.

Assignment4:

JDBC Application to display Emp details based on empId

--------------------------------------------------------------------

Dt : 13/10/2021

Assignment1:(Solution)

JDBC Application to display user details based on uname and pword.

package test;

import java.util.*;

import java.sql.*;

public class DBCon6 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

System.out.println("Enter the UserName:");


String uName = s.nextLine();

System.out.println("Enter the PassWord:");

String pWord = s.nextLine();

PreparedStatement ps = con.prepareStatement

("select * from UserReg38 where uname=? and pword=?");

//Compilation

ps.setString(1,uName);

ps.setString(2,pWord);

ResultSet rs = ps.executeQuery();//Execution

if(rs.next())

System.out.println(rs.getString(3)+"\t"+

rs.getString(4)+"\t"+rs.getString(5)+"\t"+rs.getString(6)

+"\t"+rs.getLong(7));

}else {

System.out.println("Invalid UserName or PassWord...");

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

}
o/p:

Enter the UserName:

nit.v

Enter the PassWord:

mzu672

Venkat M SR Nagar v@gmail.com 9898981234

--------------------------------------------------------------------------

Exp program:

JDBC Application to update Product price based on pCode.

package test;

import java.util.*;

import java.sql.*;

public class DBCon7 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

System.out.println("Enter the Product Code:");

String pCode = s.nextLine();

PreparedStatement ps1=con.prepareStatement

("select * from Product38 where pcode=?");


ps1.setString(1,pCode);

ResultSet rs = ps1.executeQuery();

if(rs.next()) {

System.out.println("Old Price:"+rs.getFloat(3));

System.out.println("Enter New Product Price:");

float price = s.nextFloat();

PreparedStatement ps2 = con.prepareStatement

("update Product38 set pprice=? where pcode=?");

ps2.setFloat(1,price);

ps2.setString(2,pCode);

int k = ps2.executeUpdate();

if(k>0) {

System.out.println("Price Updated...");

}else {

System.out.println("Invalid Prod Code...");

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

}
o/p:

Enter the Product Code:

A121

Old Price:123.45

Enter New Product Price:

234.56

Price Updated...

---------------------------------------------------------------------

Assignment:

JDBC Application to update book qty based on book code?

-------------------------------------------------------------

Exp program:

JDBC Application to delete Product details based on pCode.

package test;

import java.util.*;

import java.sql.*;

public class DBCon8 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
System.out.println("Enter the Product Code:");

String pCode = s.nextLine();

PreparedStatement ps1=con.prepareStatement

("select * from Product38 where pcode=?");

ps1.setString(1,pCode);

ResultSet rs = ps1.executeQuery();

if(rs.next()) {

PreparedStatement ps2 = con.prepareStatement

("delete from Product38 where pcode=?");

ps2.setString(1,pCode);

int k = ps2.executeUpdate();

if(k>0) {

System.out.println("Product details deleted");

}else {

System.out.println("Invalid Prod Code...");

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

}
o/p:

Enter the Product Code:

A124

Product details deleted

------------------------------------------------------------------

Dt : 18/10/2021

*imp

3.CallableStatement:

=>'CallableStatement' is an interface from java.sql package

and which is used to execute 'Procedures and Functions' on

DataBase product.

define Procedure?

=>procedure is a set-of-queries executed on DataBase product

and after execution it will not return any value.

structure of procedure:

create or replace procedure Proc_name

(para_list) is

begin

query1;

query2;

...

end;
/

define Function?

=>Function is a set-of-queries executed on DataBase product and

after execution it will return the value.

Note:

=>Functions in SQL will use 'return' statement to return the

value after execution.

structure of Function:

create or replace function Func_name

(para_list)return data_type as var data_type;

begin

query1;

query2;

....

return var;

end;

Dt : 19/10/2021

*imp

Constructing and Executing Stored Procedures on DB Product:


step-1 : Create the following DB tables

Bank38(accno,custname,balance,acctype)

create table Bank38(accno number(15),custname varchar2(15),

balance number(10,2),acctype varchar2(15),primary key(accno));

CustDetails38(accno,addr,mid,phno)

create table CustDetails38(accno number(15),addr varchar2(15),

mid varchar2(25),phno number(15),primary key(accno));

step-2 : Construct Procedure to insert data to DB Tables

create or replace procedure CreateAccount38

(a number,b varchar2,c number,d varchar2,e varchar2,

f varchar2,g number) is

begin

insert into Bank38 values(a,b,c,d);

insert into CustDetails38 values(a,e,f,g);

end;

step-3 : Construct JDBC Application to execute Procedure


o/p:

Enter the AccNo:

6123456

Enter the CustName:

Raj

Enter the Balance:

12000

Enter the AccType:

Savings

Enter the Address:

SRN

Enter the MailId:

r@gmail.com

Enter the PhNo:

9898981234

Procedure Executed Successfully...

---------------------------------------------------

Note:

=>we use prepareCall() method from 'Connection' interface to

create the implementation object of 'CallableStatement' interface.

Method Signature of prepareCall() method:


public abstract java.sql.CallableStatement prepareCall

(java.lang.String) throws java.sql.SQLException;

syntax:

CallableStatement cs = con.prepareCall("Proc_name/Func_name");

-------------------------------------------------------------

Assignment1:

Step-1 : DB table

EmpDetails38(eid,ename,edesg)

EmpAddress38(eid,hno,sname,city,pincode)

EmpContact38(eid,mid,phno)

EmpSalary38(eid,bsal,totSal)

totSal = bSal + HRA +DA;

HRA = 93% of bSal

DA = 63% of bSal
step-2 : Construct procedure to insert data to above DB tables

step-3 " Construct JDBC Application to execute procedure

------------------------------------------------------

Dt : 20/10/2021

Assignment1:(Solution)

EmpDetails38(eid,ename,edesg)

create table EmpDetails38(eid varchar2(10),ename varchar2(15),

edesg varchar2(10),primary key(eid));

EmpAddress38(eid,hno,sname,city,pincode)

create table EmpAddress38(eid varchar2(10),hno varchar2(15),

sname varchar2(15),city varchar2(15),pincode number(10),

primary key(eid));

EmpContact38(eid,mid,phno)

create table EmpContact38(eid varchar2(10),mid varchar2(25),

phno number(15),primary key(eid));

EmpSalary38(eid,bsal,totSal)
create table EmpSalary38(eid varchar2(10),bsal number(10),

totSal number(10,2),primary key(eid));

Procedure to insert data into DB tables:

create or replace procedure EmpData38

(eid varchar2,ename varchar2,edesg varchar2,hno varchar2,

sname varchar2,city varchar2,pincode number,mid varchar2,

phno number,bsal number,totSal number) is

begin

insert into EmpDetails38 values(eid,ename,edesg);

insert into EmpAddress38 values(eid,hno,sname,city,pincode);

insert into EmpContact38 values(eid,mid,phno);

insert into EmpSalary38 values(eid,bsal,totSal);

end;

JDBC Application:

o/p:

Enter the EmpId

A121

Enter the EmpName

Raj
Enter the EmpDesg

SE

Enter the HNo:

12-34/h

Enter the StreetName:

SRN

Enter the City:

Hyd

Enter the pinCode

612345

Enter the MailId:

r@gmail.com

Enter the PhNo:

9898981234

Enter the bSal:

12000

Procesure executed Successfully...

--------------------------------------------------------

EID ENAME EDESG

---------- --------------- ----------

A121 Raj SE

EID HNO SNAME CITY PINCODE

---------- --------------- --------- ------------- ----------


A121 12-34/h SRN Hyd 612345

EID MID PHNO

---------- ------------------------- ----------

A121 r@gmail.com 9898981234

EID BSAL TOTSAL

---------- ---------- ----------

A121 12000 30720

=============================================================

Assignment-2:

Step-1 : Construct DB tables

StuDetails38(rollNo,stuname,branch)

StuAddress38(rollNo,hno,sname,city,pincode)

StuContact38(rollno,mid,phno)

StuResult38(rollNo,totMarks,per,result)

Note:

read 6 Sub marks and calculate the following,

totMarks

per

result
Step-2 : Construct Procedure to insert values to DB tables

Step-3 : Construct JDBC Application to execute Procedure.

==========================================================

Types of Procedures:

=>Procedures are categorized into two types:

(a)IN Parameter Procedures

(b)OUT Parameter Procedures

(a)IN Parameter Procedures:

=>The Procedures which take the data from JavaPrograms and

update DataBase tables are known as 'IN Parameter Procedures'.

ex:

above programs

(b)OUT Parameter Procedures:

=>The Procedures which take the data from DB Tables and send

to the Java Programs are known as 'OUT Parameter Procedures'.

ex program:

Step-1 : Construct OUT-Parameter procedure to retrieve CustData

based on 'accNo'.

create or replace procedure CustDataRetrieve38

(ano number,cname OUT varchar2,bal OUT number,


atype OUT varchar2,ad OUT varchar2,md OUT varchar2,

pno OUT number) is

begin

select custname,balance,acctype into cname,bal,atype from

Bank38 where accno=ano;

select addr,mid,phno into ad,md,pno from CustDetails38

where accno=ano;

end;

step-2 : Construct JDBC Application to execute Procedure

package test;

import java.util.*;

import java.sql.*;

public class DBCon11 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

System.out.println("Enter the AccNo:");

long accNo = Long.parseLong(s.nextLine());


CallableStatement cs = con.prepareCall

("{call CustDataRetrieve38(?,?,?,?,?,?,?)}");

cs.setLong(1,accNo);

cs.registerOutParameter(2,Types.VARCHAR);

cs.registerOutParameter(3,Types.FLOAT);

cs.registerOutParameter(4,Types.VARCHAR);

cs.registerOutParameter(5,Types.VARCHAR);

cs.registerOutParameter(6,Types.VARCHAR);

cs.registerOutParameter(7,Types.BIGINT);

cs.execute();//Executing Procedure

System.out.println("AccNo:"+accNo);

System.out.println("CustName:"+cs.getString(2));

System.out.println("Balance:"+cs.getFloat(3));

System.out.println("AccType:"+cs.getString(4));

System.out.println("Address:"+cs.getString(5));

System.out.println("MailId:"+cs.getString(6));

System.out.println("PhNo:"+cs.getLong(7));

s.close();

}catch(Exception e) {

System.out.println(e.getMessage());

o/p:
Enter the AccNo:

6123456

AccNo:6123456

CustName:Raj

Balance:12000.0

AccType:Savings

Address:SRN

MailId:r@gmail.com

PhNo:9898981234

You might also like