Unit 4 Important Topics
Unit 4 Important Topics
Statement PreparedStatement
It is used when SQL query is to be It is used when SQL query is to be executed
executed only once. multiple times.
Used to execute normal SQL queries. Used to execute dynamic SQL queries.
We can not use statement for reading We can use Preparedstatement for reading
binary data. binary data.
We can not use statement for writing We can use Preparedstatement for writing
binary data. binary data.
JDBC Drivers:
JDBC Driver is a so ware component that enables java applica on to interact with the
database. There are 4 types of JDBC drivers:
1) Type I or JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC
bridge driver converts JDBC method calls into the ODBC func on calls. This is now discouraged
because of thin driver.
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:
easy to use.
can be easily connected to any database.
Disadvantages:
Performance degraded because JDBC method call is converted into the ODBC
func on calls.
The ODBC driver needs to be installed on the client machine.
Advantage:
No client side library is required because of applica on server that can perform many
tasks like audi ng, load balancing, logging etc.
Disadvantages:
Advantage:
//Step 3 & 4 – Establish a connec on using the Connec on class objectRegister the
drivers using DriverManager
Connection con = DriverManager.getConnection(
"jdbc:derby://localhost:1527/testDb","username", "password");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String job = rs.getString("job");
System.out.println(id+" "+name+" "+job);
}
} catch(SQLException e) {
System.out.println("SQL exception occured" + e);
}
}
}
Result:
The above code sample will produce the following result. The result may vary.
id name job
1 alok trainee
2 ravi trainee
Example 2: create, alter & drop SQL commands to create, edit or delete table.
import java.sql.*;
Result:
The above code sample will produce the following result. The result may vary.
Example 3: update, delete & insert SQL commands to edit or delete row contents.
import java.sql.*;
stmt.execute(query1);
stmt.execute(query2);
stmt.execute(query3);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String job = rs.getString("job");
System.out.println(id+" "+name+" "+job);
}
} catch(SQLExcep on e) {
System.out.println("SQL excep on occured" + e);
}
}
}
Result:
The above code sample will produce the following result. The result may vary.
id name job
2 ravi trainee
1 ronak manager
JavaBeans:
A JavaBean is a Java class that should follow the following conven ons:
package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
How to access the JavaBean class?
To access the JavaBean class, we should use ge er and se er methods.
package mypack;
public class Test{
public sta c void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//se ng value to the object
System.out.println(e.getName());
}}
JavaBean Proper es
A JavaBean property is a named feature that can be accessed by the user of the object. The
feature can be of any Java data type, containing the classes that you define.
A JavaBean property may be read, write, read-only, or write-only. JavaBean features are
accessed through two methods in the JavaBean's implementa on class:
1. getPropertyName ()
For example, if the property name is firstName, the method name would be getFirstName()
to read that property. This method is called the accessor.
2. setPropertyName ()
For example, if the property name is firstName, the method name would be setFirstName()
to write that property. This method is called the mutator.
Advantages of JavaBean
The following are the advantages of JavaBean:
The JavaBean proper es and methods can be exposed to another applica on.
It provides an easiness to reuse the so ware components.
Disadvantages of JavaBean
The following are the disadvantages of JavaBean:
JavaBeans are mutable. So, it can't take advantages of immutable objects.
Crea ng the se er and ge er method for each property separately may lead to the
boilerplate code.
jsp:useBean ac on tag:
The jsp:useBean ac on tag is used to locate or instan ate a bean class. If bean object of
the Bean class is already created, it doesn't create the bean depending on the scope. But
if object of bean is not created, it instan ates the bean.
Syntax of jsp:useBean ac on tag
<jsp:useBean id= "instanceName" scope= "page | request | session | applica on"
class= "packageName.className" type= "packageName.className"
beanName="packageName.className | <%= expression >" >
</jsp:useBean>