【发布时间】: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