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

📄 commentdao.java

📁 有发布
💻 JAVA
字号:
package DB;

import java.util.ArrayList;
import bean.ConnectionBean;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import bean.*;
import java.sql.PreparedStatement;
import bean.commentBean;

public class commentDAO {
    public commentDAO() {
    }

    //数据插入方法
    public boolean instercomment(commentBean cbean) {

        Connection con = null;
        PreparedStatement stmt = null;

        try {
            ConnectionBean dbcon = new ConnectionBean();
            con = dbcon.getConnection();
            stmt = con.prepareStatement(
                    "insert into newscomment(Comment,Stars,name) values(?,?,?)");

            stmt.setString(1, cbean.getComment());
            stmt.setString(2, cbean.getStars());
            stmt.setString(3, cbean.getName());
            stmt.executeUpdate();
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;

        } finally {
            try {
                stmt.close();
                con.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }


    //获取所有评论的信息
    public ArrayList Selectcomment() {
        ArrayList list = new ArrayList();
        ConnectionBean conbean = new ConnectionBean();
        Connection conn = null;
        ResultSet ret = null;
        PreparedStatement stmt = null;
        try {

            conn = conbean.getConnection();
            stmt = conn.prepareStatement(
                    "select * from newscomment order by CreatTime desc");
            ret = stmt.executeQuery();

            while (ret.next()) {
                commentBean bean = new commentBean();
                bean.setID(ret.getString("ID"));
                bean.setComment(ret.getString("comment"));
                bean.setStars(ret.getString("Stars"));
                bean.setCreatTime(ret.getString("CreatTime"));
                bean.setName(ret.getString("name"));
                list.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ret.close();
                stmt.close();
                conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return list;
    }

    /*删除评论*/
    public boolean delcomment(commentBean cbean) {
        boolean flag = false;
        ConnectionBean dbcon = new ConnectionBean();
        Connection conn = dbcon.getConnection();
        PreparedStatement pstmt = null;
        try {

            //根据ID删除评论表中的数据
            pstmt = conn.prepareStatement(
                    "delete from newscomment where ID = '" + cbean.getID() +
                    "'");
            pstmt.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

    }

}

⌨️ 快捷键说明

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