Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
182 views

Enterprise Java Notes

The 3 interfaces provided by JDBC API to execute SQL statements are: 1. Statement Interface: - Used to execute basic SQL statements like SELECT, INSERT, UPDATE, DELETE. - Not recommended for parameterized statements as it is prone to SQL injection. 2. PreparedStatement Interface: - Used to execute precompiled SQL statements with or without IN parameters. - Parameters are set using setXXX methods and then executed. - Provides better performance over Statement interface. 3. CallableStatement Interface: - Used to execute stored procedures with IN, OUT and INOUT parameters. - Parameters are registered using registerOutParameter and setXXX methods before execution. So in summary, the JDBC

Uploaded by

Lakhan Vasnani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Enterprise Java Notes

The 3 interfaces provided by JDBC API to execute SQL statements are: 1. Statement Interface: - Used to execute basic SQL statements like SELECT, INSERT, UPDATE, DELETE. - Not recommended for parameterized statements as it is prone to SQL injection. 2. PreparedStatement Interface: - Used to execute precompiled SQL statements with or without IN parameters. - Parameters are set using setXXX methods and then executed. - Provides better performance over Statement interface. 3. CallableStatement Interface: - Used to execute stored procedures with IN, OUT and INOUT parameters. - Parameters are registered using registerOutParameter and setXXX methods before execution. So in summary, the JDBC

Uploaded by

Lakhan Vasnani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Enterprise Java

Q) What is Enterprise application.


1) An enterprise application is computing model in web environment.
2) An enterprise application is basically business application.
Q) What is Java Enterprise Edition.
1) The Java Enterprise Edition (Java EE) is a collection of Java APIs owned by Oracle.
2) It is also known as Java 2 platform Enterprise Edition (J2EE).
3) Java EE platform is designed to help developers create large-scale, multi-tiered, scalable,
reliable & secure network applications.
4) Java EE extends Java Standard Edition (Java SE).
5) Java EE is specifically designed for enterprise applications/business where we have to
deal with number of different servers with importance on security, transaction
management etc.
6) J2EE platform consists of set of services, APIs, and protocols that provide functionality
for developing web-based application.

Q) Java EE Technologies.
Q) Java EE Architecture.
Q) Java EE containers.
1) Containers are the interface between a component and the low-level, platform-specific
functionality that supports the component.
2) J2EE containers provide runtime support for J2EE application components.
3) J2EE application components use methods of the container to access other application
components.
4) Containers provide services for Security, Transaction, Availability, Lifecycle Management.
There are 5 defined container types:
a) EJB container:
i) EJB container acts as interface between enterprise beans and the clients.
ii) It manages the execution of enterprise beans for Java EE applications.
iii) Enterprise beans and their container run on the Java EE server.

b) Web Container:
i) Web container is the interface between web components and the web server.
ii) It manages the execution of JSP page and servlet components for Java EE applications.
iii) Web components and their container run on the Java EE server.

c) Application client container:


i) The application client container is the interface between Java EE application clients and the
Java EE server.
ii) It manages the execution of application client components.
iii) Application clients and their container run on the client.
d) Applet container:
i) It Manages the execution of applets.
ii) It consists of a web browser and Java Plug-in running on the client together.

Q) What is JDBC.
1) JDBC stands for Java Database Connectivity.
2) JDBC is a software component that enables us to interact with the database.
3) JDBC acts as a interface between the client and a database server.
4) With the help of JDBC, we can connect with different types of databases.
5) The JDBC classes are contained in the Java Package java.sql and javax.sql.
6) For example, JDBC drivers enables to open database connections and to interact with it by
sending SQL or database commands.
Types of JDBC drivers:
Type 1: JDBC-ODBC Bridge driver (Bridge)
i) In a Type 1 driver, a JDBC bridge uses ODBC driver to connect to the database.
ii) The ODBC bridge driver is needed to be installed on each client machines.
iii) Type-1 driver is also called Universal driver because it can be used to connect to any of
the databases.
iv) Sun provides a JDBC-ODBC Bridge driver by “sun.jdbc.odbc.JdbcOdbcDriver”.

Advantages:
 Easy to use.
 It can be connected to any database.

Disadvantages:
 ODBC Drivers needs to be installed on client machine.

Type 2: JDBC native-API


i) The Native API driver uses the client -side libraries of the database.
ii) These drivers are typically provided by the database vendors and used in the same
manner as the JDBC-ODBC Bridge. The vendor-specific driver must be installed on each
client machine.
iii) In a Type 2 driver, JDBC method calls into native calls of the database API the database.

Advantages:
 Performance upgraded than JDBC-ODBC bridge driver.

Disadvantages:
 Native Driver needs to be installed on each client machine.
 Vendor client library needs to be installed on client machine.
Type 3: JDBC-Net pure Java
i) In a Type 3 driver, The Network Protocol driver uses middleware (application server) that
converts JDBC calls directly or indirectly into the vendor-specific database protocol.
ii) It is also known as Pure Java driver for database-middleware.
iii) Here all the database connectivity drivers are present in a single server, hence no need of
individual client-side installation.
iv) It is fully written in Java.

Advantages:
 This driver is fully written in Java and hence Portable.
 It is suitable for the web.
 They are the most efficient amongst all driver types.

Disadvantages:
 Network support is required on client machine.

Type 4: 100% Pure Java(Thin Driver)


i) In a Type 4 driver, a pure Java-based driver communicates directly with the vendor's
database through socket connection. This is the highest performance driver available for
the database and is usually provided by the vendor itself.
ii) This driver interact directly with database
iii) It does not require any native database library, that is why it is also known as Thin Driver
Advantages:
 Better performance than all other drivers.
 No software is required on client machine.

Disadvantages:
 Driver depends on the database.

Q) JDBC Architecture.
Q) JDBC Connectivity.
Steps to connect:
Import JDBC packages.
Define the connection URL.
Established the connections.
Create the statement object.
Execute a query.
Process the results.
Close the connections.

Import JDBC packages:


import java.sql.* ; // for standard JDBC programs
import java.math.* ; // for BigDecimal and BigInteger support
Define the connection URL: “Class.forName();”
 For JDBC-ODBC Bridge Driver:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

 For Oracle driver:


Class.forName("oracle.jdbc.driver.OracleDriver");
 For MySQL Driver:
Class.forName("com.mysql.jdbc.Driver”);

Established the connections:


Connection con=DriverManager.getConnection(“url”,user_name”,”pass”);
Example--
String URL = "jdbc:oracle:thin:@amrood:1521:EMP";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL,USER,PASS);

Create the statement object:


Statement st=con.createStatement();

Execute a Query:
 For Select query-
String sql=”SELECT * FROM EMP”;
st.executeQuery(sql);

 For Insert/Update query-


String sql=INSERT into EMP values(1,”lucky”);
St.executeUpdate(sql);

 For Delete query-


String sql=”DELETE from EMP WHERE ID<102”;
St.executeUpdate(sql);

Process the results.


ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt(Id));
System.out.println(rs.getString(name));
}

Close the connections.


st.close();
con.close();

Program:
Class.forName("com.mysql.jdbc.Driver”);
String URL=”local/host/EMP”;
Connection con=DriverManager.getConnection(URL);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“SELECT * FROM EMP”);
while(rs.next())
{
System.out.println(rs.getInt(Id));
System.out.println(rs.getString(name));
}
con.close();

Program:
import java.sql.*;
import java.util.*;
class Example{
public static void main(String args[])throws Exception
{
class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp_record","root"," ");
System.out.println("Connection Established");

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from traning");

System.out.println("Rollno Student_Name Stream Percentage");


System.out.println("------------------------------------------");
while(rs.next())
{
System.out.println(rs.getInt(1)+ " " +rs.getString(2)+ " " +rs.getString(3)+ " "+rs.getFloat(4));
}
} }

Program:
try{
Class.forName("com.mysql.jdbc.Driver”); //load driver
Connection con=DriverManager.getConnection( //connecting to database
“jdbc:odbc:mydatabasename”,”userlogin”,”password”);
Statement st=con.createStatement(); //creating statement
ResultSet rs=st.executeQuery(“SELECT * FROM EMP”); //iterate through result &
while(rs.next()) prints students name
{
System.out.println(rs.getInt(Id));
System.out.println(rs.getString(name));
}
st.close();
con.close();
}
catch(SQLException e){}
catch(ClassNotFoundException c){}

Program:
Import java.sql;
class SimpleExample {
public static void main(String args[]) {
String url = “jdbc:odbc:mysource”;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection myConnection =
DriverManager.getConnection(url,”Bond”,”TopSecret”);
myConnection.close(); }
catch(java.lang.Exception) { ex.printStackTrace(); }
}

ResultSet
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“select fname,salary from EMP”);
while(rs.next())
{
System.out.println(rs.getString(1)+”:”); //printing the results
System.out.println(rs.getDouble(“salary”));
}

UPDATE
while(rs.next())
{
System.out.println(rs.getString(“Firstname”));
}
rs.previous();
rs.updateString(“”Firstname”,args[0]);
rs.updateRow();
INSERT
rs.movetoInsertRow();
rs.updateString(“FIRST NAME”,”lucky”);
rs.updateString(“LAST NAME”,”vasnani”);
rs.updateInt(“ID”,101);
rs.InsertRow();

DELETE
rs.last();
rs.deleteRow();

rs.previous(); / / go back in the RS (not possible in JDBC 1…)


rs.relative(-5); / / go 5 records back
rs.relative(7); / / go 7 records forward
rs.absolute(100); / / go to 100th record

Q) JDBC API provides 3 different interfaces to execute different SQL Queries:


 Statements
 Prepared statements
 Callable statements

A) Statements:
1) It is used to execute normal SQL queries.
2) We cannot pass the parameters to SQL query using this interface.
3) It is mainly used for DDL statements like CREATE, ALTER, DROP etc.
4) It is base interface.
Statement st=con.createStatement();

B) Prepared:
1) It is used to execute dynamic or parametrized SQL Queries.
2) We can pass the parameters to SQL query at runtime using this interface.
3) It is used for any kind of SQL queries which are to be executed multiple times.
4) It extends Statement interface.
5) The PreparedStatement accepts runtime values and these values can be passed as
parameters. For example, the values of EMPID, EMPNAME and EMPSAL can be taken from
GUI and passed as parameter values
PreparedStatement psmt=con.PreparedStatement();

C) CallableStatement:
1) It is used to execute the Stored Procedures.
2) You can pass 3 types of parameters using this interface. They are – IN, OUT and IN OUT.
3) It is used to execute stored procedures and functions.
4) It extends PreparedStatement Interface
Stored Procedure has 3 types of parameters
i) IN: IN parameter is used to provide input values.
ii) OUT: OUT parameter is used to collect output values.
iii) INOUT: It is used to provide input and to collect output values.
CallableStatement csmt=con.prepareCall();

You might also like