JDBC 增改查

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);
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中使用JDBC进行增删改查操作的示例代码: 1. 导入必要的包: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; ``` 2. 建立数据库连接: ```java String url = "jdbc:mysql://localhost:3306/mydb"; String user = "username"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); ``` 3. 插入数据: ```java String insertQuery = "INSERT INTO mytable (column1, column2) VALUES (?, ?)"; PreparedStatement insertStmt = conn.prepareStatement(insertQuery); insertStmt.setString(1, value1); insertStmt.setString(2, value2); insertStmt.executeUpdate(); ``` 4. 更新数据: ```java String updateQuery = "UPDATE mytable SET column1 = ? WHERE column2 = ?"; PreparedStatement updateStmt = conn.prepareStatement(updateQuery); updateStmt.setString(1, newValue); updateStmt.setString(2, conditionValue); updateStmt.executeUpdate(); ``` 5. 删除数据: ```java String deleteQuery = "DELETE FROM mytable WHERE column1 = ?"; PreparedStatement deleteStmt = conn.prepareStatement(deleteQuery); deleteStmt.setString(1, conditionValue); deleteStmt.executeUpdate(); ``` 6. 查询数据: ```java String selectQuery = "SELECT * FROM mytable WHERE column1 = ?"; PreparedStatement selectStmt = conn.prepareStatement(selectQuery); selectStmt.setString(1, conditionValue); ResultSet resultSet = selectStmt.executeQuery(); while(resultSet.next()) { String value1 = resultSet.getString("column1"); String value2 = resultSet.getString("column2"); // 处理查询结果 } ``` 7. 关闭连接: ```java resultSet.close(); selectStmt.close(); conn.close(); ``` 请确保替换代码中的数据库连接信息、SQL查询语句和参数值,并根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值