怎么判断集合里面有没有对应的id
来源:10-2 项目作业
宝慕林4199460
2023-03-09 19:43:53
package com.imooc.jdbc.newsapp.command; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.imooc.jdbc.newsapp.entity.News; import org.apache.commons.dbutils.QueryRunner; import javax.sql.DataSource; import java.io.FileInputStream; import java.net.URLDecoder; import java.sql.Connection; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; import java.util.Properties; import java.util.Scanner; /** *修改新闻 */ public class UpdateCommand implements Command { @Override public List<News> execute() { //修改新闻前,先展示所有新闻。调用查询命令,显示所有数据。 Command command = new QueryCommand(); List<News> list=command.execute(); Scanner in = new Scanner(System.in); boolean i = true; Integer id = 0; while (i){ System.out.println("请输入要修改新闻的id"); id = in.nextInt(); // 后面更新的时候用到id,所以放在while前面声明。 //判断新闻id是否存在,如果不存在,重写输入 for(News news : list){ if (news.getId() == id){ i = false; break; } System.out.println("不存在该新闻id,请重新输入!"); } } System.out.println("请输入新闻标题"); String title = in.next(); System.out.println("请输入新闻内"); String content = in.next(); // Apache DBUtils + Druid 联合使用 Properties properties = new Properties(); String propertyFile = UpdateCommand.class.getResource("/druid-config.properties").getPath(); Connection conn = null; try { propertyFile = new URLDecoder().decode(propertyFile, "UTF-8"); properties.load(new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); conn.setAutoCommit(false); String sql = "update news set title = ? , content = ? where id = ?"; QueryRunner qr = new QueryRunner(); qr.update(conn,sql,new Object[]{title,content,id}); conn.commit(); System.out.println("修改成功!"); } catch (Exception e) { e.printStackTrace(); try { if (conn!=null && !conn.isClosed()) { conn.rollback(); } } catch (SQLException ex) { e.printStackTrace(); } }finally { try { if (conn!=null && !conn.isClosed()){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } return list; } }
问题描述:
我这代码怎么实现不了,itellij工具我不会单步调试。下面这两个我看不懂,大佬救命
3.UpdateCommand类中可以调用QueryCommand类中的execute方法,并且判断list集合中的元素。在删除之前先将符合要求的元素进行存储,删除之后返回存储了被删除元素的List<News> list
4.DeleteCommand类的实现方式同第三条
1回答
好帮手慕小蓝
2023-03-10
同学你好,老师将需要修改的部分直接写在了代码中,并且提供了注释,同学可以参考一下。
package com.imooc.jdbc.newsapp.command; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.imooc.jdbc.newsapp.entity.News; import org.apache.commons.dbutils.QueryRunner; import javax.sql.DataSource; import java.io.FileInputStream; import java.net.URLDecoder; import java.sql.Connection; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Scanner; /** *添加新闻 */ public class UpdateCommand implements Command { @Override public List<News> execute() { //修改新闻前,先展示所有新闻。调用查询命令,显示所有数据。 Command command = new QueryCommand(); List<News> list=command.execute(); Scanner in = new Scanner(System.in); boolean i = true; Integer id = 0; //将被修改的News对象进行存储 News updated = null; while (i){ System.out.println("请输入要修改新闻的id"); id = in.nextInt(); // 后面更新的时候用到id,所以放在while前面声明。 //判断新闻id是否存在,如果不存在,重写输入 for(News news : list){ if (news.getId() == id){ i = false; //将符合条件的News对象进行存储 updated = news; break; } } //打印语句应当写在循环外,并且需要进行判断 if (i) System.out.println("不存在该新闻id,请重新输入!"); } //将被更新的元素放在集合中,最终返回这个集合即可 List<News> updateNews = new ArrayList<>(); updateNews.add(updated); System.out.println("请输入新闻标题"); String title = in.next(); System.out.println("请输入新闻内"); String content = in.next(); // Apache DBUtils + Druid 联合使用 Properties properties = new Properties(); String propertyFile = UpdateCommand.class.getResource("/druid-config.properties").getPath(); Connection conn = null; try { propertyFile = new URLDecoder().decode(propertyFile, "UTF-8"); properties.load(new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); conn.setAutoCommit(false); String sql = "update news set title = ? , content = ? where id = ?"; QueryRunner qr = new QueryRunner(); qr.update(conn,sql,new Object[]{title,content,id}); conn.commit(); System.out.println("修改成功!"); } catch (Exception e) { e.printStackTrace(); try { if (conn!=null && !conn.isClosed()) { conn.rollback(); } } catch (SQLException ex) { e.printStackTrace(); } }finally { try { if (conn!=null && !conn.isClosed()){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } return updateNews; } }
祝学习愉快~
相似问题