>  기사  >  Java  >  서블릿의 DAO 레이어를 작성하는 방법

서블릿의 DAO 레이어를 작성하는 방법

(*-*)浩
(*-*)浩원래의
2019-05-16 14:18:117724검색

Dao 레이어: Dao 레이어는 데이터 액세스 레이어라고 하며, 이는 데이터 액세스 개체를 의미하며 특정 테이블이나 엔터티의 추가, 삭제, 수정 및 쿼리를 포함하는 비교적 낮은 수준의 기본 작업입니다.

추천 과정: Java 튜토리얼.

서블릿의 DAO 레이어를 작성하는 방법

Dao 레이어
먼저 인터페이스 클래스를 선언하고, 클래스에서 사용할 몇 가지 메소드를 선언하고,
이 인터페이스 클래스를 구현하는 동일한 레이어에 클래스를 작성하고, 인터페이스 클래스의 메소드를

다시 작성합니다. 구현 Mybatis 작성 방법
은 주로 데이터를 처리하는 방법입니다.

public interface IStuClassDao {
    //全表查询方法
    public List findAllStuClassInfo();
    //classID查询
    public Map<String, Object> findStuClassById(int classId) ;
    //增加方法
    public void addStuClassById(Stuclass sc) ;
    //更新方法
    public void updateStuClassById(Stuclass sc) ;
    //查询方法
    public String findClassNamesByIds(String ids);
}

사용자 작업을 예로 들어 보겠습니다.

Mybatis의 작성 방법은 구현되지 않습니다

AnimalDAO:

package DAO;


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


import util.JDBCUtil;
import entry.Animal;
/**
 * 对数据库进行操作
 * @author dell-
 *
 */
public class AnimalDAO {


	//添加动物信息
	public void addAnimal(Animal animal){
		//1建立连接
		Connection conn= JDBCUtil.getConnection();
		//2创建sql语句
		String sql = "insert into animal (aid,aname,atime)values(?,?,?)";
		//3创建sql执行对象
		PreparedStatement ps =null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, animal.getAid());
			ps.setString(2, animal.getAname());
			ps.setDate(3, new java.sql.Date(animal.getAtime().getTime()));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		JDBCUtil.release(null,ps,conn);
		}
	}
	//查询所有信息
	public List<Animal> getAll(){
		List<Animal> list = new ArrayList<Animal>();
		//1连接数据库
		Connection conn = JDBCUtil.getConnection();
		//2拼装sql
		String sql="select * from animal";
		//3创建sql执行对象
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				Animal animal = new Animal();
				animal.setAid(rs.getInt("aid"));
				animal.setAname(rs.getString("aname"));
				animal.setAtime(rs.getDate("atime"));
				list.add(animal);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}

		return list;
	}
	//通过aid 删除动物信息
	public void deleteAnimal(int aid){
		//1建立数据库连接
		Connection conn = JDBCUtil.getConnection();
		//2拼装sql
		String sql = "delete from animal where aid=?";
		//3创建sql执行对象
		PreparedStatement ps =null;
		try {
		ps = conn.prepareStatement(sql);
		ps.setInt(1, aid);
		ps.executeUpdate();
		} catch (SQLException e) {
		e.printStackTrace();
		}finally{
		JDBCUtil.release(null, ps, conn);
		}
		}
		//通过aid修改动物信息
		public void updateAnimal(Animal animal){
			//1建立连接
			Connection conn = JDBCUtil.getConnection();
			//2拼装sql
			String sql = "update animal set aname=?,atime=? where aid=?";
			//3创建sql执行对象
			PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, animal.getAname());
			ps.setDate(2, new java.sql.Date(animal.getAtime().getTime()));
			ps.setInt(3, animal.getAid());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.release(null, ps, conn);
		}
	}
	public Animal getAnimalByid(int aid){
		//1链接数据库
		Connection conn= JDBCUtil.getConnection();
		//2创建sql语句
		String sql = "select * from animal where aid=?";
		//3创建sql执行对象
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, aid);
			rs = ps.executeQuery();
			if(rs.next()){
				Animal animal = new Animal();
				animal.setAid(rs.getInt("aid"));
				animal.setAname(rs.getString("aname"));
				animal.setAtime(rs.getDate("atime"));
				return animal;
			}
		} catch (SQLException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
		return null;
	}
}

위 내용은 서블릿의 DAO 레이어를 작성하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.