【发布时间】:2018-11-20 05:53:57
【问题描述】:
我有一个 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 中
我的 doGet 是
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,那么它会抛出错误。请有任何知识的人帮助我
【问题讨论】:
-
错误是什么?
-
顺便说一句,在编写更多类似的 SQL 查询之前,您可能需要研究“SQL 注入”
-
检查您的数据库是否有有效的
mt_distributr_vcdistributrcode -
@cricket_007 在 servlet 中当我调用 Outlet.Outlet() 时它说要传递一个参数我应该传递什么参数?什么是 SQL 注入?
-
@ScaryWombat 什么??我没有得到你
标签: java sql servlets jdbc methods