0% found this document useful (0 votes)
113 views29 pages

Class Running Notes 10th To 18th August

The document defines various concepts related to applications and databases in Java. It discusses what an application is, defines web applications, and explains the technologies used to build web applications like JDBC, Servlets, and JSP. It then describes different types of storage in Java like field storage, object storage, file storage, and database storage. It discusses establishing a connection between a Java application and a database using JDBC. It provides steps to install Oracle database and configure the environment for a JDBC application. Finally, it shows an example of connecting to an Oracle database and retrieving records using JDBC.

Uploaded by

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

Class Running Notes 10th To 18th August

The document defines various concepts related to applications and databases in Java. It discusses what an application is, defines web applications, and explains the technologies used to build web applications like JDBC, Servlets, and JSP. It then describes different types of storage in Java like field storage, object storage, file storage, and database storage. It discusses establishing a connection between a Java application and a database using JDBC. It provides steps to install Oracle database and configure the environment for a JDBC application. Finally, it shows an example of connecting to an Oracle database and retrieving records using JDBC.

Uploaded by

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

Dt : 10/8/2021

Define Application?

=>The set-of-programs collected together to perform defined action is known as Application.

define WebApplications?

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

as WebApplication.

=>we use the following technologies to construct WebApplications:

1.JDBC

2.Servlet

3.JSP

Diagram:
-----------------------------------------------------------------------------

*imp

1.JDBC:(Part-1)

=>JDBC stands for 'Java DataBase Connectivity' and which is used to establish

communication(link) b/w JavaProgram and DataBase product.

=>The following are the types of Storages related to Java Application:

(a)Field Storage

(b)Object Storage

(c)File Storage

(d)DataBase Storage

(a)Field Storage:

=>The memory which is created part of JavaApplication to hold Single data value is known

as Field Storage.

=>These Field Storages will be created when we use Primitive DataTypes like byte,short,

int,long,float,double,char and boolean.

Exp:

int k = 10;

(b)Object Storage:

=>The memory which is created part of JavaApplication to hold group members is known as

Object Storage.

=>These Object Storages will be created when we use NonPrimitive DataTypes like class,
interface,Array and Enum.

Exp:

class Addition

int a,b;

void add()

System.out.println("Sum:"+(a+b));

Addition ad = new Addition();

Diagram:
Note:

=>The Field Storages and the Object Storages which are created part of JVM while

application execution will be destroyed automatically when JVM ShutDowns,because of this

reason these are Temporary Storages.

=>Java Applications need Permanent Storages,because of this reason we use the following:

=>File Storage

=>DataBase Storage

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

Dt : 11/8/2021

(c)File Storage:

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

by the Operating System is known as File Storage.

Note:

=>In the process of establishing communication b/w JavaApp and File Storage,the JavaApp

must be constructed using the classes and interfaces available from 'java.io' package.
Diagram:

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

DisAdvantages of File Storage:

1.Data Redundancy

2.Data Inconsistency

3.Difficulity in Accessing Data

4.Limited Data Sharing

5.Integrity Problems

6.Atomicity problems

7.Concurrent Access Problems

8.Security Problems.
1.Data Redundancy:

=>Same information may be duplicated in different files,which leads to Data Redundancy.

2.Data Inconsistency:

=>Because of Data Redundancy,the data may not be in Consistent state.

3.Difficulity in Accessing Data:

=>Accessing data is not convenient.

4.Limited Data Sharing:

=>Data Sharing is difficult,because the data is available in Scattered form.

5.Integrity Problems:

=>Combining Scattered data is difficult.

6.Atomicity problems:

=>Atomicity means complete the transaction entirely or not at all,which is not posible

if File Storage.

7.Concurrent Access Problems:

=>Multiple Users accessing data simultaneously is not posible in File Storage.

8.Security Problems:

=>Security is very less in File Storage.


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

Note:

=>Because of these DisAdvantages,we cannot have File Storage as BackEnd for

WebApplications.

=>These DisAdvantages of FileStorage can be overcomed using DataBase Storage.

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

Dt : 12/8/2021

faq:

define API?

=>API stands for 'Application Programming Interface' and which is collection of 'classes

and Interfaces' used to construct applications.

=>According to Java 'API' means package,which is collection of Classes and Interfaces.

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

*imp

(d)DataBase Storage:

=>The largest permanent Storage of ComputerSystem,which is installed from externally is

known as DataBase Storage.

Note:

=>In the process of establishing communication b/w JavaApp and DB Storage,the JavaApp must

be constructed using 'classes and Interfaces' available from 'java.sql' package and the

JavaApp must use 'Jdbc Driver'.

Diagram:
----------------------------------------------------------------------------------------

faq:

define 'driver'?

=>The short s/w program used by OperatingSystem to control resources of ComputerSystem.

Exp:

Audio driver

Video driver

N/w driver

...

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

faq:

define 'Jdbc driver'?

=>JDBC driver is used to establish connection b/w JavaApp and DataBase Storage.

(JDBC Driver - Java DataBase Connectivity Driver)

=>These 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)

Note:

=>In realtime we use 'Thin driver(Type-4)' to establish communication b/w JavaApp and

DataBase Storage.

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

Dt : 13/8/2021

*imp

Installing DataBase Product(Oracle) and making the system Environment ready for executing

JDBC Applications:

step-1 : Download and Install DataBase Product(Oracle)

step-2 : Check the Login process

sql>connect

Enter user-name : system

Enter password : manager

(press enter)

step-3 : create one table with name "Employee35"

create table Employee35(eid varchar2(10),ename varchar2(15),edesg varchar2(10),

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

step-4 : Insert min five records into DB Table

insert into Employee35 values('A121','Raj','SE',120000);

-------------------------------------------------------------------
step-5 : Dowload DB Jar file related to DB Product

(JAR - Java Archeive,and which is compressed format of 'class files')

oracle10 - ojdbc14.jar

oracle11 - ojdbc6.jar

oracle12 - ojdbc7.jar,ojdbc8.jar

other - UPC.jar(Universal Connection Pool)

Note:

=>we can find DB Jar file in "lib" folder of DB Product

C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar

MySql - mysql-connector-java-VERSION.jar

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

step-6 : Copy this DB Jar file into 'ext' folder of JDK

C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext

Note:

=>If 'ext' folder is not available then create one user defined folder with any name in

any location and copy with DB Jar file.

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

step-7 : Find the DB Product PortNo and ServiceName

=>To know PortNo and ServiceName,open 'tnsnames.ora' file from 'ADMIN' as follows:

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

Port No : 1521

Service Name : XE

=====================================================================================
Dt : 14/8/2021

*imp

we use the following steps to establish communication b/w JavaProgram and DB Product:

step-1 : Loading Driver

step-2 : Creating Connection

step-3 : Preparing Statements

step-4 : Executing Query

step-5 : Closing Connection

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

*imp

Creating JDBC Application using IDE Eclipse:

1.Open IDE Eclipse,while opening name the WorkSpace and click ok or Launch.

2.Create Java Project

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

and click finish.

3.Add DB Jar file to the project through 'Build-path'

RightClick on JavaProject->Build path->configure Build path->Libraries->select 'classpath'

and click Add External Jars->Browse and select DB Jar file from 'ext' folder or select from

user defined folder->open->Apply->'Apply and close'.

4.Create packages in 'src'

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

5.Create class in package

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

6.Write the following code to display the records from the DB Table 'Employee35'
package test;

import java.sql.*;

public class DBCon1 {

public static void main(String[] args) {

try {

Class.forName("oracle.jdbc.driver.OracleDriver"); //step-1

Connection con = DriverManager.getConnection

("jdbc:oracle:thin:@localhost:1521:XE","system","manager");//step-2

Statement stm = con.createStatement();//step-3

ResultSet rs = stm.executeQuery("select * from Employee35");//step-4

while(rs.next())

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

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

}//end of loop

con.close();//step-5

}catch(Exception e) {e.printStackTrace();}

7.Execute the JDBC Application

o/p:

A121 Raj SE 120000


A122 Alex TE 110000

A123 Asok ME 120000

A124 Ram SE 140000

A125 REX ME 150000

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

DT : 16/8/2021

step-1 : Loading Driver

=>The process of loading JDBC driver to current running program is known as Loading Driver.

=>we use forName() method from java.lang.Class to load driver.

Method Signature:

public static java.lang.Class<?> forName(java.lang.String)

throws java.lang.ClassNotFoundException;

syntax:

Class c = Class.forName("DriverClass");

(or)

Class.forName("DriverClass");

Note:

=>forName() method will load the driverClass at runtime or executioin.

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

step-2 : Creating Connection

=>The processing of establishing link(connection) b/w JavaProgram and DB product is known

as Creating Connection.

=>we use getConnection() method from 'java.sql.DriverManager' class to create connection.

Method Signature:
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

faq:

define 'Connection'?

'Connection' is an interface from java.sql packge and which provides the following

some important methods:

=>createStatement()

=>prepareStatement()

=>prepareCall()

=>close()
Execution behaviour of getConnection() method:

=>getConnection() method will create the implementation object of 'Connection' interface

and the object is linked to DB product.

=>This object reference is copied on to reference variable(con) and the object will hold

the methods 'Connection' interface.

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

Step-3 : Preparing Statements

=>The process of Specifying the actions to be performed is known as Preparing Statements.

=>According to JDBC the statements are categorized into three types:

(i)Statement

(ii)PreparedStatement

(iii)CallableStatement

(i)Statement:

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

normal queries.

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

implementation object of 'Statement' interface.

Method Signature:

public abstract java.sql.Statement createStatement() throws java.sql.SQLException;

syntax:

Statement stm = con.createStatement();

=>'Statement' will provide the following two important methods:

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

(a)executeQuery():

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

Method Signature:

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

throws java.sql.SQLException;

syntax:

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

(b)executeUpdate():

=>executeUpdate() method is used to execute quries like create,insert,update and delete.

Method Signature:

public abstract int executeUpdate(java.lang.String) throes java.sl.SQLException;

syntax:

int k = stm.executeUpdate("query");

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

Dt : 17/8/2021

step-4 : Executing Query

=>The process of running query on DB product is known as Executing Query process.

=>we use executeQuery() and executeUpdate() methods to run queries on DB Product.

step-5 : Closing Connection

=>The process of closing the DB Connection is known as Closing Connection.

=>we use close() method from 'Connection' interface to perform closing operation.

Method Signature:

public abstract void close() throws java.sql.SQLException;


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

faq:

define ResultSet?

=>'ResultSet' is an interface from java.sql package and which is used to hold the result

generated from 'select' queries.

=>we use executeQuery() method to create the implementation object of 'ResultSet' interface

and this object will hold result of select query.

=>we use 'next()' method from 'ResultSet' to move the corsor row-by-row,when the row(record)

is availbale the next() method generates true,if not it is false.

=>we use the following 'getter methods' from 'ResultSet' to retrieve the data based on

column_no or column_name.

Diagram:(flow diagram of JDBC_App1)

===============================================================================
Assignment1:

step1 : Create DB Table with name 'Book35' from SQLCommandLine.

(bcode,bname,bauthor,bprice,bqty)

primary key : bcode

step2 : Insert min 5 records from SQLCommandLine.

step3 : Construct JDBC Application to display all the Book details

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

Exp program:

step1 : create DB table with name 'Product35'

(pcode,pname,pprice,pqty)

primary key : pcode

create table Product35(pcode varchar2(10),pname varchar2(15),pprice number(10,2),

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

step2 : Construct JDBC application to read product details from Console(Keyboard) and insert

into DB Table 'Product35'.

DBCon2.java

package test;

import java.sql.*;

import java.util.*;

public class DBCon2 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);


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

String pCode = s.nextLine();

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

String pName = s.nextLine();

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

float pPrice = s.nextFloat();

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

int pQty = s.nextInt();

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

Connection con = DriverManager.getConnection

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

Statement stm = con.createStatement();

int k = stm.executeUpdate

("insert into Product35 values('"+pCode+"','"+pName+"',"+pPrice+","+pQty+")");

if(k>0) {

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

con.close();

s.close();

}catch(Exception e) {e.printStackTrace();}

o/p:

Enter the ProdCode:

A111
Enter the ProdName:

Mouse

Enter the ProdPrice:

1200

Enter the ProdQty:

12

Product details inserted Successfully....

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

Note:

=>In the above program 'k' will hold the value equal to the number of records updated in

DB Table.

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

Assignment2:

Construct JDBC Application to insert Emp details into DB Table 'Employee35'.

Assignment3:

Construct JDBC Application to insert Book details into DB Table 'Book35'.

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

Dt : 18/8/2021

JDBC Statements:

=>JDBC Statements are categorized into three types:

(i)Statement

(ii)PreparedStatement

(iii)CallableStatement
(i)Statement:

=>'java.sql.Statement' is used to execute normal queries.

syntax:

Statement stm = con.createStatement();

Exp:

above programs

(ii)PreparedStatement:

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

normal queries with IN parameters.(Parameterized queries)

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

implementation object of 'PreparedStatement' interface.

Method Signature:

public abstract java.sql.PreparedStatement prepareStatement(java.lang.String)

throws java.sql.SQLException;

syntax:

PreparedStatement ps = con.prepareStatement("query-structure");

=>The following are some important methods of PreparedStatement:

(a)executeQuery()

(b)executeUpdate()

(a)executeQuery():

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


Method Signature:

public abstract java.sql.ResultSet executeQuery() throws java.sql.SQLException;

syntax:

ResultSet rs = ps.executeQuery();

(b)executeUpdate():

=>executeUpdate() method is used to execute quries like create,insert,update and delete.

Method Signature:

public abstract int executeUpdate() throes java.sl.SQLException;

syntax:

int k = ps.executeUpdate();

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

Example program:

step1 : Create DB Table with name 'Book35' from SQLCommandLine.

(bcode,bname,bauthor,bprice,bqty)

primary key : bcode

create table Book35(bcode varchar2(10),bname varchar2(15),bauthor varchar2(15),

bprice number(10,2),bqty number(10),primary key(bcode));

step2 :Construct JDBC Application to read data from Console and insert into DB Table 'Book35'

(using PreparedStatement)(Single record insertion program)

DBCon3.java

package test;

import java.sql.*;
import java.util.*;

public class DBCon3 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

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

String bCode = s.nextLine();

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

String bName = s.nextLine();

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

String bAuthor = s.nextLine();

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

float bPrice = s.nextFloat();

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

int bQty = s.nextInt();

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

Connection con = DriverManager.getConnection

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

PreparedStatement ps = con.prepareStatement

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

ps.setString(1,bCode);

ps.setString(2,bName);

ps.setString(3, bAuthor);
ps.setFloat(4,bPrice);

ps.setInt(5, bQty);

int k = ps.executeUpdate();

if(k>0) {

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

con.close();

s.close();

}catch(Exception e) {e.printStackTrace();}

o/p:

Enter the BCode:

A121

Enter the BName:

C-Lang

Enter the BAuthor:

B-Swamy

Enter the BPrice:

567.89

Enter the BQty:

10

BookDetails Inserted Successfully...

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

Example program:
Construct JDBC Application to insert multiple BookDetails based on User Choice?

DBCon4.java

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");

PreparedStatement ps = con.prepareStatement

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

System.out.println("Enter the number BookDetails to be inserted:");

int n = Integer.parseInt(s.nextLine());

for(int i=1;i<=n;i++) {

System.out.println("Enter the BCode"+i);

String bCode = s.nextLine();

System.out.println("Enter the BName"+i);

String bName = s.nextLine();

System.out.println("Enter the BAuthor"+i);

String bAuthor = s.nextLine();


System.out.println("Enter the BPrice"+i);

float bPrice = Float.parseFloat(s.nextLine());

System.out.println("Enter the BQty"+i);

int bQty = Integer.parseInt(s.nextLine());

ps.setString(1,bCode);

ps.setString(2,bName);

ps.setString(3, bAuthor);

ps.setFloat(4,bPrice);

ps.setInt(5, bQty);

int k = ps.executeUpdate();

if(k>0) {

System.out.println(i+"BookDetails Inserted Successfully...");

}//end of loop

con.close();

s.close();

}catch(Exception e) {e.printStackTrace();}

o/p:

Enter the number BookDetails to be inserted:

Enter the BCode1


A122

Enter the BName1

CJ

Enter the BAuthor1

XYZ

Enter the BPrice1

1200

Enter the BQty1

12

1BookDetails Inserted Successfully...

Enter the BCode2

A123

Enter the BName2

AJ

Enter the BAuthor2

PQR

Enter the BPrice2

1300

Enter the BQty2

23

2BookDetails Inserted Successfully...

Enter the BCode3

A124

Enter the BName3

HB
Enter the BAuthor3

ABC

Enter the BPrice3

2345.67

Enter the BQty3

21

3BookDetails Inserted Successfully...

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

Exp program:

Construct JDBC Application to display book details based on bCode?

DCon5.java

package test;

import java.sql.*;

import java.util.*;

public class DBCon5 {

public static void main(String[] args) {

try {

Scanner s = new Scanner(System.in);

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

String bCode = s.nextLine();

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

Connection con = DriverManager.getConnection

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

PreparedStatement ps = con.prepareStatement
("select * from Book35 where bcode=?");

ps.setString(1,bCode);

ResultSet rs = ps.executeQuery();

if(rs.next()) {

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

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

}else {

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

con.close();

s.close();

}catch(Exception e) {e.printStackTrace();}

o/p:

Enter the bCode:

A123

A123 AJ PQR 1300.0 23

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

You might also like