Enterprise Java Notes
Enterprise Java Notes
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.
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.
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.
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.
Execute a Query:
For Select query-
String sql=”SELECT * FROM EMP”;
st.executeQuery(sql);
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");
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();
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();