Dao 작업 추출 - BaseDao 캡슐화
1. SQL 문 쓰기
2. 연결 가져오기
3. PreparedStatement 만들기
4. sql 문장 실행하기
a) 업데이트 b) 질의
5. 닫기/예외
BaseDao의 구현
BaseDao는 자신이 쓴 통용적인dao 부류입니다. 자신이 쓴 모든dao는 이 부류dao를 계승합니다.
통용 방법
update
, delete
, insert
package com.eu.dss.dao;
import com.eu.dss.util.ConnUtil;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import java.sql.*;
import java.util.ArrayList;
/**
* Created by on 2017/5/18.
*/
public class BaseDao {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
/**
*
* sql (update/insert/delete)
* paramsValue sql ( , null)
*/
public void update(String sql, Object[] paramsValue) {
try {
//
conn = ConnUtil.getConnextion();
//
pstmt = conn.prepareStatement(sql);
// :
int count = pstmt.getParameterMetaData().getParameterCount();
//
if (paramsValue != null && paramsValue.length > 0) {
//
for (int i = 0; i < count; i++) {
pstmt.setObject(i + 1, paramsValue[i]);
}
}
pstmt.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
ConnUtil.close(null, pstmt, conn);
}
}
/**
*
*/
public List query (String sql, Object[] paramsValue , Class tClass){
List list = new ArrayList();
//
conn = ConnUtil.getConnextion();
try {
//
pstmt = conn.prepareStatement(sql);
int count = pstmt.getParameterMetaData().getParameterCount();
if(paramsValue !=null && paramsValue.length> 0 ){
for(int i=0;i
DAO 계층: 우리가 Dao 계층의 코드를 쓸 때 매우 간단합니다.
다음 예는 다음과 같습니다.
package com.eu.dss.dao.impl;
import com.eu.dss.dao.BaseDao;
import com.eu.dss.dao.ITronClassDao;
import com.eu.dss.entity.TronClasstype;
import net.sf.json.JSONArray;
import java.util.List;
/**
* Created by on 2017/5/23.
*/
public class TronClassDao extends BaseDao implements ITronClassDao {
public List TronClasstype() {
String sql = " SELECT * FROM eu_tronclass ; ";
List list = super.query(sql,null,TronClasstype.class);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println("bbbb"+jsonArray);
return list;
}
public TronClasstype findByid(int id) {
String sql = " SELECT * FROM eu_tronclass where id=? ; ";
List list = super.query(sql,new Object[id],TronClasstype.class);
return (list!=null && list.size()>0) ? list.get(0) : null;
}
public void save(TronClasstype tronClassType) {
String sql = " INSERT INTO eu_tronclass (year,tron_month,eu_rj,eu_xin,eu_rw,eu_ts,eu_xiu,eu_gz,eu_kuai,eu_ad,eu_wc,eu_wu,eu_jr) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?); ";
Object[] paramsValue = {tronClassType.getYear(),tronClassType.getTron_month(),
tronClassType.getEu_rj(), tronClassType.getEu_xin(),
tronClassType.getEu_rw(), tronClassType.getEu_ts(),
tronClassType.getEu_xiu(), tronClassType.getEu_gz(),
tronClassType.getEu_kuai(), tronClassType.getEu_ad(),
tronClassType.getEu_wc(), tronClassType.getEu_wu(),
tronClassType.getEu_jr()};
super.update(sql,paramsValue);
}
public void update(TronClasstype tronClassType ) {
String sql = " UPDATE eu_tronclass SET year = ?,tron_month =?,eu_rj =?," +
"eu_xin =?,eu_rw=?,eu_ts=?,eu_xiu=?,eu_gz=?,eu_kuai=?," +
"eu_ad=?,eu_wc=?,eu_wu=?,eu_jr=? where id=?";
Object[] paramsValue = {tronClassType.getYear(),tronClassType.getTron_month(),
tronClassType.getEu_rj(), tronClassType.getEu_xin(),
tronClassType.getEu_rw(), tronClassType.getEu_ts(),
tronClassType.getEu_xiu(), tronClassType.getEu_gz(),
tronClassType.getEu_kuai(), tronClassType.getEu_ad(),
tronClassType.getEu_wc(), tronClassType.getEu_wu(),
tronClassType.getEu_jr(), tronClassType.getId()};
super.update(sql,paramsValue);
}
public void delete(int id) {
String sql = " delete from eu_tronclass where id =? ";
Object[] paramsValue = {id};
super.update(sql,paramsValue);
}
}
문장 문집:JavaEE--학습 노트
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.