jdbc避免sql注入漏洞的方法:
使用PreparedStatement來避免SQL注入,PreparedStatement繼承了Statement接口,執行SQL語句的方法無異,例如:
//預編譯SQL語句
PreparedStatement pstmt = conn.prepareStatement ("select * from user where username = ? and password = ?") ;
//為參數下標賦值
pstmt.setString (1,username);
pstmt.setString(2,password);
//執行SQL語句,接受結果
ResultSet resultSet = preparedStatement.executeQuery();