haoxiaolan 2022-07-05 16:33 采纳率: 35.3%
浏览 29
已结题

java链接mysql

 if(choice  == 1){
    System.out.println("请输入要删除的学号");
     int  no = scanner.nextInt();
     sql = " delete from students where sno = " +no+ ' ';
            }

   sql = " delete from students where sno = "+no+ ' ';
  //这句sql语句写成下面这样 删除就失效了?
   sql = " delete from students where sno =  '"+no+ "'";
原因是啥呀?
  • 写回答

2条回答 默认 最新

  • it_hao528 2022-07-05 17:03
    关注

    如果 no = 1;
    sql = " delete from students where sno = "+no+ ' '; ==> delete from students where sno = 1
    sql = " delete from students where sno = '"+no+ "'"; ==> delete from students where sno = '1'
    因为sno需要的是数字 1,而下边的语句变成了字符串 '1' 所以会执行失败了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月12日
  • 已采纳回答 7月12日
  • 创建了问题 7月5日