Javatpoint Logo

what do u mean by DAO?plz expalin in details

By: sima12*** On: Sun Mar 03 20:19:56 IST 2013     Question Reputation5 Answer Reputation0 Quiz Belt Series Points0  5Blank User
ssssUp5Down

 
Dao clases are used to reuse the jdbc logic & Dao(Data Access Object) is a design pattern.
dao is a simple java class which contains JDBC logic .

Data Access Layer has proven good in separate business logic layer and persistent layer. The DAO design pattern completely hides the data access implementation from its clients
Image Created5Down

By: [email protected] On: Sun Mar 03 21:23:45 IST 2013 Question Reputation0 Answer Reputation5 Belt Series Points0 5User Image
Are You Satisfied :6Yes3No
 
The Java Data Access Object (Java DAO) is an important component in business applications. Business applications almost always need access to data from relational or object databases and the Java platform offers many techniques for accessingthis data. The oldest and most mature technique is to use the Java Database Connectivity (JDBC)API, which provides the capability to execute SQL queries against a databaseand then fetch the results, one column at a time.

Image Created0Down

By: [email protected] On: Mon Mar 04 13:08:09 IST 2013 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :3Yes2No
 
package model;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class CustomerBean implements java.io.Serializable{

private DataSource ds;
private String customerId;

public CustomerBean(){
try{
Context naming = new InitialContext();
ds = (DataSource) naming.lookup("jdbc/SalesDB");
}catch(NamingException e){
throw new RuntimeException(e);
}
}

public final String getCustomerId(){
return customerId;
}

public boolean authorize(String customerId, String password){
try{
Connection con = ds.getConnection();
try{
PreparedStatement pstmt = con.prepareStatement(
"select count(cust_id) from customers where "
+ "cust_id=? and pwd=?");
pstmt.setString(1, customerId);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
int count = rs.next() ? rs.getInt(1) : 0;
rs.close();
pstmt.close();
this.customerId = count == 1 ? customerId : null;
return count == 1;
}finally{
con.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}
}

public int placeOrder(int productNo, int quantity){
try{
Connection con = ds.getConnection();
con.setAutoCommit(false);
try{
Statement stmt = con.createStatement();
stmt.executeUpdate("update ord_ctl set ord_no=ord_no+1");
ResultSet rs = stmt.executeQuery(
"select ord_no from ord_ctl");
rs.next();
int orderNo = rs.getInt("ord_no");
rs.close();
stmt.close();
Date today = new Date(System.currentTimeMillis());
PreparedStatement pstmt = con.prepareStatement(
"insert into orders values(?,?,?,?,?)");
pstmt.setInt(1, orderNo);
pstmt.setDate(2, today);
pstmt.setString(3, customerId);
pstmt.setInt(4, productNo);
pstmt.setInt(5, quantity);
pstmt.executeUpdate();
pstmt.close();
con.commit();
return orderNo;
}catch(SQLException e){
con.rollback();
throw e;
}finally{
con.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
Image Created0Down

By: [email protected] On: Mon Mar 04 16:35:16 IST 2013 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :2Yes2No