sql如何将java类的参数化方法调用为servlet doget方法
我有一个java类,其中有一个返回json的方法,我想将该方法调用到servlet doGet方法中,以便稍后进行AJAX调用
但是在调用java类方法(Outlet.Outlet)时,它要求传递一个参数,我不知道传递什么 请检查一下我的代码
这是我的java类
public class Outlet {
static Connection con = null;
static Statement statement = null;
ResultSet resultSet = null;
public static String Outlet(String idDB) throws ClassNotFoundException, SQLException {
List<String> list = new ArrayList<String>();
con = DBConnection.createConnection();
statement = con.createStatement();
String sql="select CUSTOMERDESCRIPTOR as OUTLETNAME from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";
System.out.println("iddb :"+idDB);
try {
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
list.add(resultSet.getString("OUTLETNAME"));
}
} catch (SQLException e) {
e.printStackTrace();
}
String json = new Gson().toJson(list);
System.out.println("Json Outlet :"+json);
return json;
}
}
在上面的java类中,我返回一个Json,我想将该方法调用到我的servlet doGost中
我的狗是
try {
String json = Outlet.Outlet(); //what should i pass here as a parameter
response.setContentType("application/json");
response.getWriter().write(json);
System.out.println("dheeraj"+json);
}
catch (Exception e) {
e.printStackTrace();
}
}
如果我正在传递idDB,那么它将抛出错误。请任何有知识的人帮助我
# 1 楼答案
请阅读OWASP - SQL Injection并了解准备好的报表
首先,方法不应该以大写字母开头,因此您可以将其命名为
Outlet.findById
,而不是Outlet.Outlet
(方法不应该与类相同;读起来非常混乱),并且可以从请求中获取参数调用API时,添加
?id=value
或者,您可以从
request
获取路径的最后一部分,假设您的API设置为/path/ids/value
-请参考What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest?以获取此选项在执行此操作之前,当然应该仔细检查正在运行的查询在直接查询数据库时是否实际返回数据