DAO设计模式

jsp只关注于数据的显示,而不关心数据是从哪里来,所以jsp中不应该使用任何sql包,数据库操作代码最好使用PreparedStatement。

j2ee的组件层次:客户端-->表示层-->业务层-->数据层

DAO属于j2ee数据层的操作,操作数据库,DAO封装了数据库中表的全部操作。

实例:

假设表:

 create table person
 (
  id varchar(32) primary key not null,
 name varchar(20) not null,
 password varchar(20) not null,
 age varchar(20) not null,
 email varchar(20) not null
 );

那么DAO则规定了操作person表或person对象的接口

针对对象插入,而对象包括值对象(VO),传输对象(TO),最根本的java对象(POJO)

客户通过VO操作DAO

DAO设计模式其实就是把数据库中的表用类描述为对象,然后规定该对象的数据库操作接口,最后实现该接口的类。

1,类描述的对象

package cn.lxh.vo;

public class Person {

 public String id;
 
 public String name;
 
 public String password;
 
 public String age;
 
 public String email;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 
 
}

2,对象操作接口

package cn.lxh.dao;

import java.util.List;
import cn.lxh.vo.Person;

public interface PersonDAO {
 //增加操作
 public void insert(Person person) throws Exception;
 //修改操作
 public void update(Person person) throws Exception;
 //删除操作
 public void delete(String id) throws Exception;
 //按id查询操作
 public void queryById(String id) throws Exception;
 //查询全部
 public List<Person> queryAll() throws Exception;
 //模糊查询
 public List<Person> queryByLike(String cond) throws Exception; 
}

3,实现该接口的类(数据库操作代码)

数据库连接/关闭类

package cn.lxh.dbc;

import java.sql.*;

public class DataBaseConnection {

 //驱动程序
 private String url="oracle.jdbc.driver.OracleDriver";
 
 //数据库连接字符串
 private String conStr="jdbc:oracle:thin:@192.168.1.113:1521:orcl";
 
 //连接对象
 private Connection con=null;
 
    //对象在实例化的时候自动连接数据库
 public DataBaseConnection() throws Exception
 {
  Class.forName(url);
  this.con=DriverManager.getConnection(conStr);
 }
 
 //取得连接
 public Connection getCon()
 {
  return this.con;
 }
 
 //关闭数据库
 public void closeDba() throws SQLException
 { 
  con.close();
 }
 
}

对象操作数据库类

package cn.lxh.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.lxh.dao.PersonDAO;
import cn.lxh.vo.Person;
import cn.lxh.dbc.*;

public class PersonDAOImpl implements PersonDAO {

 public void delete(String id) throws Exception {

 }

 public void insert(Person person) throws Exception {
       String sql="insert into person(id,name,password,age,email) values(?,?,?,?,?) ";
       Connection con=null;
       DataBaseConnection conn =null;
       try
       {
   conn = new DataBaseConnection();
   con= conn.getCon();
   PreparedStatement prepare = con.prepareStatement(sql);
   prepare.setString(0, "1");
   prepare.setString(1, "jin");
   prepare.setString(2, "123");
   prepare.setString(3, "14");
   prepare.setString(4, "932654256@qq.com");
   prepare.executeUpdate();
   prepare.close();
  }
         catch (Exception e) {
   throw new Exception("出现错误!");
  }
  finally
  {
   conn.closeDba();
  }
 }

 public List<Person> queryAll() throws Exception {
  return null;
 }

 public void queryById(String id) throws Exception {
         String sql="select * from person where id=?";
         DataBaseConnection con=null;
         try {
   con = new DataBaseConnection();
   Connection conn = con.getCon();
   PreparedStatement preState = conn.prepareStatement(sql);
   preState.setString(0, "1");
   ResultSet rs = preState.executeQuery();
   if(rs.next()) {
    Person person = new Person();
    person.setId(rs.getString(1));
   }
   preState.close();
  } catch (Exception e) {
   throw new Exception("出现错误!");
  }finally
  {
   con.closeDba();
  }
 }

 public List<Person> queryByLike(String cond) throws Exception {
  List<Person> list=new ArrayList<Person>();
  String sql="select * from person where name like ?";
         DataBaseConnection con=null;
         try {
   con = new DataBaseConnection();
   Connection conn = con.getCon();
   PreparedStatement preState = conn.prepareStatement(sql);
   preState.setString(0, "%"+cond+"%");
   ResultSet rs = preState.executeQuery();
   while (rs.next()) {
    Person person = new Person();
    person.setId(rs.getString(1));
    list.add(person);
   }
   preState.close();
  } catch (Exception e) {
   throw new Exception("出现错误!");
  }finally
  {
   con.closeDba();
  }
  return list;
 }

 public void update(Person person) throws Exception {

 }

}

jsp页面代码:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="cn.lxh.dao.*"%>
<%@ page import="cn.lxh.dao.impl.*"%>
<%@ page import="cn.lxh.vo.*"%>
<%@ page import="cn.lxh.dbc.*"%>

<%
 Person person=new Person();
 person.setId("jin");
 PersonDAO dao=new PersonDAOImpl();
 try
 {
  dao.insert(person);
 }
 catch(Exception e)
 {
   throw new Exception("出现错误!");
 }
%>

原文地址:https://www.cnblogs.com/jinzhengquan/p/1951323.html