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

📄 messagedao.java

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

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

public class messageDAO {
    public messageDAO() {}

    //向数据库中插入留言信息
    public boolean instermessage(messageBean cbean) {

        Connection con = null;
        PreparedStatement stmt = null;

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

            stmt.setString(1, cbean.getMessage());
            stmt.setString(2, 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 Selectmessage() {
        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 newsmessage order by CreatTime desc");
            ret = stmt.executeQuery();

            while (ret.next()) {
                messageBean bean = new messageBean();
                bean.setID(ret.getString("ID"));
                bean.setMessage(ret.getString("Message"));
                bean.setCreatTime(ret.getString("CreatTime"));
                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 delmessage(messageBean bean) {
        // boolean flag = false;
        ConnectionBean dbcon = new ConnectionBean();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {

            conn = dbcon.getConnection();
            pstmt = conn.prepareStatement(
                    "delete from newsmessage where ID = '" + bean.getID() +
                    "'");
            pstmt.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                //关闭所有连接
                pstmt.close();
                conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

    }


}

⌨️ 快捷键说明

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