MySQL尝试更新MySql表的语法,但语法已关闭

时间:2017-10-03 13:14:53

标签: java html mysql

目前这是我更新我的sql的代码,我收到了语法错误。辫子从前端传入。任何人都可以帮我修复语法。

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){}

    }
}

2 个答案:

答案 0 :(得分:1)

将sql查询行更改为

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

答案 1 :(得分:0)

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

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