老师检查一下
问题描述:1.修改的sql语句能一次改两个属性吗,例如,但是报错了,老师能示范一下吗
String sql = "update news set (title =?,content=?) where id=?";
问题描述:2.输入id后会单独显示一行你输入的id的新闻,不会单独显示出来
相关截图:
相关代码:
package chapter10_2.command; import java.util.Scanner; public class newsTest { public static void main(String[] args) { boolean flag = true; Scanner scanner = new Scanner(System.in); Command command = null; do { System.out.println("欢迎来到新闻管理系统"); System.out.println("======================="); System.out.println(" 1 添加新闻 "); System.out.println(" 2 查看新闻 "); System.out.println(" 3 编辑新闻 "); System.out.println(" 4 删除新闻 "); System.out.println(" 5 退出系统 "); System.out.println("请输入1-5之间的数字进行操作"); int sc = scanner.nextInt(); if (sc > 5 || sc < 1) { System.out.println("输入错误,请用户重新输入:"); } else { switch (sc) { case 1: command = new InsertNewsCommand(); command.execute(); break; case 2: command = new QueryNewsCommand(); command.execute(); break; case 3: command = new EditNewsCommand(); command.execute(); break; case 4: command = new DeleteNewsCommand(); command.execute(); break; case 5: System.out.println("退出成功!"); flag = false; //System.exit(0); break; } } } while (flag); } } package chapter10_2.command; import chapter10_2.enity.news; import com.alibaba.druid.pool.DruidDataSourceFactory; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Properties; import java.util.Scanner; public class EditNewsCommand implements Command{ public void query(){ Connection conn = null; Properties properties = new Properties(); String propertyFlie = EditNewsCommand.class.getResource("/druid-config.properties").getPath(); try { propertyFlie = URLDecoder.decode(propertyFlie,"UTF-8"); properties.load(new FileInputStream(propertyFlie)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); QueryRunner qr = new QueryRunner(dataSource); List<news> list = qr.query("select * from news ", new BeanListHandler<>(news.class)); for(news n : list){ System.out.println(n.toString()); } } catch (Exception e) { e.printStackTrace(); } } @Override public void execute() { Connection conn = null; query(); Scanner scanner = new Scanner(System.in); System.out.println("请输入要修改的新闻id"); Integer id = scanner.nextInt(); System.out.println("请输入要修改的新闻标题"); String title = scanner.next(); System.out.println("请输入要修改的新闻内容"); String content = scanner.next(); Properties properties = new Properties(); String propertyFlie = EditNewsCommand.class.getResource("/druid-config.properties").getPath(); try { propertyFlie = URLDecoder.decode(propertyFlie,"UTF-8"); properties.load(new FileInputStream(propertyFlie)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); conn.setAutoCommit(false); String sql = "update news set title =? where id=?"; String sql1 = "update news set content=? where id = ?"; QueryRunner qr = new QueryRunner(); qr.update(conn,sql,new Object[]{title,id}); qr.update(conn,sql1,new Object[]{content,id}); conn.commit(); System.out.println("修改成功!"); } catch (Exception e) { e.printStackTrace(); try { if(conn != null && !conn.isClosed()){ conn.rollback(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
13
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕小小
2021-11-23 13:55:58
同学你好
1、是可以的,例如:
System.out.println("请输入新闻标题:"); String title = input.next(); System.out.println("请输入新闻内容:"); String content = input.next(); sql = "update news set title= ?, content= ? where id = ?"; int update = qr.update(conn, sql, new Object[]{title, content, id}); if (update==0){ System.out.println("id不存在修改失败"); }else{ System.out.println("修改成功!"); }
2、很抱歉,老师没能很好理解同学的问题“输入id后会单独显示一行你输入的id的新闻,不会单独显示出来”,建议再具体描述下问题。
3、建议同学上传代码是选中对应的代码语言,避免上传后代码格式混乱。
祝学习愉快
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星