t1441332354 2018-05-18 06:29 采纳率: 71.8%
浏览 2053
已采纳

如何从jsp页面向mysql按条件更新数据

代码如下

 <% 
    String ID = new String(request.getParameter("dId").getBytes("ISO8859_1"),"UTF-8");
    String AIS = new String(request.getParameter("AIS").getBytes("ISO8859_1"),"UTF-8");
    try{
        Class.forName("com.mysql.jdbc.Driver");
        String url = "XXXXXXXXX";
        String username = "XXXXXXX";
        String password = "XXXXXXX";
                Connection conn = DriverManager.getConnection(url,username,password);
                String isExist = "select * from newshipinfo where newshipinfo.AIS = '"+AIS+"'";
                PreparedStatement pstmt = conn.prepareStatement(isExist);
                ResultSet rs = pstmt.executeQuery();
                if(rs.next()){
                    out.print("该数据已存在,请不要重复添加!");
                }else{              
                String sql = "insert into newshipinfo(AIS) values (?) where ID = '"+ID+"'";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1,AIS);
                int row = ps.executeUpdate();
                if(row >0){
                    out.print("数据添加成功");
                }
                ps.close();
                conn.close();
            }
    }catch(Exception e){
        out.print("数据添加失败!");
        e.printStackTrace();
    }
%>

这个页面的目的是将前一个页面所提交的数据(ID号与AIS的值)填写到数据库里进行数据更新,数据库里AIS的初始值均为0,现在把页面的值传进去变成1或者2。传之前会判断数据库里AIS的值是不是1或者2,若是则提示不要重复输入。
现在值可以传到这个页面没有问题,就是无法传入数据库,老是报这个错

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where ID = '12259'' at line 1

是sql语句的错误吗,想请教下怎么修改代码。

  • 写回答

4条回答 默认 最新

  • javamokeyjjj 2018-05-18 06:41
    关注

    更新数据不是应该用update 表名 set 字段 where 条件 。 insert 接where就是语法错误。如果你想校验此id是否在数据库已经存在,存在修改,不存在添加。那你可以了解一下 replace into 的用法

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名