use*_*722 0 html sql database jsp
根据用户的输入,我想从数据库中选择记录.这是我的代码:
<%
String jempid=request.getParameter("empid");
out.println(jempid);
int intempid=1223;
Connection conn=null;
String url="jdbc:mysql://localhost/employees";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url,"root","");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from empdetails where empnum=jempid");
%>
Run Code Online (Sandbox Code Playgroud)
它会引发以下错误
javax.servlet.ServletException:java.sql.SQLException:'where子句中的未知列'jempid'
使用字符串连接构造SQL是一个坏主意 - 您只是打开自己的SQL注入攻击 - 特别是考虑到您直接从请求获得"empid"的值.哎呀!
更好的方法是使用参数化查询,如下所示:
PreparedStatement st=conn.prepareStatement("select * from empdetails where empnum=?");
st.setString(1, jempid);
ResultSet rs=st.executeQuery();
Run Code Online (Sandbox Code Playgroud)
你还应该检查jempid是否为空.
| 归档时间: |
|
| 查看次数: |
8644 次 |
| 最近记录: |