JDBC 增改查

本文提供了一个使用Java进行数据库操作的示例,包括获取建议记录、创建及更新建议记录的方法实现。通过连接数据源,执行SQL语句来完成数据的读取、插入与更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public List<SuggestCommandView> getSuggests()
    {


        Connection connection = null;
        try {
            List<SuggestCommandView>list = new ArrayList<SuggestCommandView>();
            final String sql = "select * from APP_SUGGEST order by UPDATETIME desc,CREATETIME desc";
            connection = dataSource.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                SuggestCommandView sug = new SuggestCommandView();
                sug.setCreateTime(resultSet.getTimestamp("CREATETIME"));
                sug.setMark(resultSet.getString("MARK"));
                sug.setSugCode(resultSet.getString("SUGCODE"));
                sug.setSugcontent(resultSet.getString("sugcontent"));
                list.add(sug);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };


    public  void  createSuggest(SuggestCommandVo vo)
    {
        Connection connection = null;
        Statement  stm = null;
        try {
            connection = dataSource.getConnection();
            stm = connection.createStatement();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
            Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
            String str = df.format(now);
            final String sql = " insert into TBL_SUGGEST  values(SUGGEST_SEQ.NEXTVAL,'"+vo.getSugCode()+"',to_timestamp('"+ str +"','yyyy-mm-dd hh24:mi:ss'),to_timestamp('"+ str +"','yyyy-mm-dd hh24:mi:ss'),'"+vo.getMark()+"','"+vo.getSugcontent()+"')";
            stm.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                try {
                    stm.close();
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    public  void  updateSuggest(SuggestCommandVo vo)
    {
        Connection connection = null;
        Statement  stm = null;
        try {
            connection = dataSource.getConnection();
            stm = connection.createStatement();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
            Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
            String str = df.format(now);
            final String sql = " update TBL_SUGGEST set SUGCODE = '"+vo.getSugCode()+"',UPDATETIME = to_timestamp('"+ str +"','yyyy-mm-dd hh24:mi:ss'),MARK='"+vo.getMark()+"',SUGCONTENT ='"+vo.getSugcontent()+"'";
            stm.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                try {
                    stm.close();
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值