⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newstypedaoimpl.java

📁 一个由jsp+servlet+MySQL简单的新闻发布系统
💻 JAVA
字号:
package com.news.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.news.bean.NewsType;
import com.news.dao.NewsTypeDao;
import com.news.util.DButil;

public class NewsTypeDaoImpl implements NewsTypeDao {

	private final String ADD = "insert into newstype values(null,?,?)";
	private final String EDIT = "update newstype set name=?,sort=? where id=?";
	private final String DELETE = "delete from newstype where id= ?";
	private final String LIST = "select * from newstype order by sort";
	private final String GET = "select * from newstype where id=?";
	private final String TOTAL = "select count(*) from newstype";

	public List<NewsType> list(Map<Object, Object> map) {
		// TODO Auto-generated method stub
		List<NewsType> list = new ArrayList<NewsType>();
		PreparedStatement pstmt = DButil.getPreparedStatement(LIST);
		try {
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				NewsType newsType = new NewsType();
				newsType.setName(rs.getString("name"));
				newsType.setSort(rs.getInt("sort"));
				newsType.setId(rs.getInt("id"));
				list.add(newsType);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return list;
	}
//	public List<NewsType> getNewsType(Map<?, ?> map) {
//	}
	public int add(NewsType nt) {
		int ret = 0, i = 1;
		try {
			PreparedStatement pstmt = DButil.getPreparedStatement(ADD);
			
			pstmt.setString(i++, nt.getName());
			pstmt.setInt(i++, nt.getSort());
			ret = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return ret;
	}
	public int edit(NewsType nt) {
		int ret = 0, i = 1;
		try {
			PreparedStatement pstmt = DButil.getPreparedStatement(EDIT);
			pstmt.setString(i++, nt.getName());
			pstmt.setInt(i++, nt.getSort());
			pstmt.setInt(i++, nt.getId());
			ret = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return ret;
	}

	public int del(Integer id) {
		int ret = 0;
		try {
			PreparedStatement pstmt = DButil.getPreparedStatement(DELETE);
			pstmt.setInt(1, id);
			ret = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return ret;
	}
	public int get(Integer id) {
		NewsType newsType = new NewsType();
		try {
			PreparedStatement pstmt = DButil.getPreparedStatement(GET);
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				newsType.setName(rs.getString("name"));
				newsType.setSort(rs.getInt("sort"));
				newsType.setId(rs.getInt("id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return 0;
	}

	public int Total() {
		int ret = 0;
		PreparedStatement pstmt = DButil.getPreparedStatement(TOTAL);
		try {
			ResultSet rs = pstmt.executeQuery();
			rs.next();
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DButil.close();
		}
		return ret;
	}





	




}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -