JDBC 增改查

來源:互聯網
上載者:User

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);
                }
            }
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.