프론트 데스크톱 dao 인터페이스와 NeCommentDao 구현
5908 단어 DAO
package cn.client.dao;
import java.util.List;
import cn.entity.NeComment;
/*
* DAO
* */
public interface NeCommentDao {
// id
public int getTotalCount(int id);
// id
public List<NeComment> getNeCommentByPage(int pageIndex, int pageSize,int id);
// id
public int addNeComment(int id, String username, String content);
//
public int getTotalCount(String username);
//
public List<NeComment> getNeCommentByStatus(int statusId);
// id
public int selectComment(int id, String username);
}
package cn.client.dao.impl;
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 cn.client.dao.BaseDao;
import cn.client.dao.NeCommentDao;
import cn.entity.NeComment;
public class NeCommentDaoImpl extends BaseDao implements NeCommentDao {
Connection conn = null;//
PreparedStatement pstmt = null;//
ResultSet rs = null;//
// id
public int getTotalCount(int id){
int total = 0;
String sql = " select count(*) from ne_comment where ne_products_id=?";
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if(rs.next()){
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return total;
}
// id
public List<NeComment> getNeCommentByPage(int pageIndex, int pageSize,int id){
int start = (pageIndex - 1) * pageSize; //
int end = pageIndex * pageSize; //
if(start>=1){
start++;
}
List<NeComment> neCommentList = new ArrayList<NeComment>();
NeComment item = null;
String sql = "SELECT * FROM ( SELECT A.*, ROWNUM RN " +
"FROM (SELECT * FROM ne_comment where " +
"ne_products_id =? and (ne_status_id=1 or ne_status_id=3) " +
"order by ne_comment.ne_time desc) A) WHERE RN BETWEEN ? AND ?";
conn=this.getConnection();
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setInt(2, start);
pstmt.setInt(3, end);
rs = pstmt.executeQuery();
while(rs.next()){
item = new NeComment(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getInt(5)
);
neCommentList.add(item);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return neCommentList;
}
// id
public int addNeComment(int id, String username, String content){
String sql = "insert into ne_comment " +
" (ne_products_id, ne_content, ne_user_id, ne_time, ne_status_id) " +
"values" +
" (?, ?, ?, sysdate, 1)";
return this.executeUpdate(sql, new Object[]{
id ,
content ,
username
});
}
//
public int getTotalCount(String username){
int total = 0;
String sql = " select count(*) from ne_comment where ne_user_id =?";
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if(rs.next()){
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return total;
}
//
public List<NeComment> getNeCommentByStatus(int statusId){
List<NeComment> neCommentList = new ArrayList<NeComment>();
NeComment item = null;
String sql = "select ne_products_id, ne_content, ne_user_id, ne_time, " +
" ne_status_id from ne_comment where ne_status_id=? " +
" order by ne_time desc";
conn=this.getConnection();
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, statusId);
rs = pstmt.executeQuery();
while(rs.next()){
item = new NeComment(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getInt(5)
);
neCommentList.add(item);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return neCommentList;
}
// id
public int selectComment(int id, String username){
int count = 0;
String sql = "select count(*) from ne_comment where ne_products_id=? and ne_user_id=?";
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, username);
rs = pstmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return count;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Spring에서 DAO가 순환 호출될 때 데이터가 실시간으로 업데이트되지 않는 해결 방법문제를 설명하기 전에 몇 가지 전제 사항을 설명하십시오. Spring의 구성 파일에서 다음과 같은 방식으로 데이터베이스 트랜잭션을 구성했다고 가정하십시오. 현재 UserDao 및 Security Service가 있습...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.