【问题标题】:Syntax for MySQL trying to update a MySql table, however syntax is offMySQL 尝试更新 MySql 表的语法,但语法已关闭
【发布时间】:2018-03-14 16:53:32
【问题描述】:

目前这是我更新 sql 的代码,但出现语法错误。 carid 是从前端传入的。任何人都可以帮助我修复语法。

String carid = req.getParameter("id");

int rs;
Connection conn = null;
java.sql.PreparedStatement st= null;
String nativeSQL = "";

try {
    Context    ctx = new InitialContext();
    Context env = ( Context )ctx.lookup( "java:comp/env" );
    DataSource ds = ( DataSource )env.lookup( "jdbc/carRentalSystem");
    conn = ds.getConnection();

    st = conn.prepareStatement("update cardetails SET Availability = Unavailable where id='"+ carid+ "'");
    st.clearParameters();
    rs= st.executeUpdate();
            if(rs != 0) {
                res.sendRedirect("carRental.jsp");
                return;
            }else {

            }
    }
    catch(Exception e) {
        e.printStackTrace();
        }
    finally {
        try{ if(st != null ) st.close(); } catch(java.sql.SQLException e){}
        try{ if(conn != null ) conn.close(); } catch(java.sql.SQLException e){}

    }
}

【问题讨论】:

  • 你从哪里得到它,它是什么?
  • id 是一个在 jsp 上从同一个表中查询的 in,Availability 是一个可用或不可用的枚举
  • 我的意思是语法错误是什么,没关系,我看到你得到了答案,即使没有发布它。请在下次发布您的问题时遇到的错误。
  • 使用 PreparedStatement - 请参阅 SQL Injection attack

标签: java html mysql


【解决方案1】:

将sql查询行改为

 st = conn.prepareStatement("update cardetails SET Availability = 'Unavailable' where id='"+ carid+ "'");

【讨论】:

  • 谢谢!这是完美的。
【解决方案2】:

字符串应该用引号括起来:

"update cardetails SET Availability = 'Unavailable' where id='"+ carid+ "'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 2015-07-30
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多