请帮助我达到以下要求。 我需要在URL中传递一个参数,该参数可以动态地从用户那里获取值,并获取select语句或过程的记录。
我尝试使用硬编码值,但我不知道如何在其中传递变量。
下面是供您参考的代码。
package notifprj;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import notifprj.ConnectionUtils;
import java.sql.CallableStatement;
import oracle.jdbc.OracleTypes;
@WebServlet(name = "DispNotif", urlPatterns = { "/dispnotif" })
public class DispNotif extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html><body>");
ResultSet rs = null;
Connection conn =null;
try {
conn = ConnectionUtils.getConnection();
Class.forName("oracle.jdbc.driver.OracleDriver");
String strQry="{call getUserDetails(?,?)}";
CallableStatement cs = conn.prepareCall(strQry);
cs.setString(1,"CHRISTIAN.MEDINA");
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
// get cursor and cast it to ResultSet
rs = (ResultSet) cs.getObject(2);
out.println("<table border=1 width=50% height=30% align=center>");
out.println("<tr><th>NOTIFICATION_ID</th><th>SUBJECT</th><th>AMOUNT</th><th>CURRENCY</th><tr>");
// loop it like normal
while (rs.next()) {
String NotifID = rs.getString("NOTIFICATION_ID");
String Subject = rs.getString("SUBJECT");
String Amount = rs.getString("AMOUNT");
String Currency = rs.getString("CURRENCY");
out.println("<tr><td>" + NotifID + "</td><td>" + Subject + "</td><td>" + Amount + "</td><td>" + Currency + "</td></tr>");
}
out.println("</table>");
out.println("</html></body>");
conn.close();
}
catch (Exception e) {
e.printStackTrace();
out.println("error");
}
}
}
答案 0 :(得分:0)
只需使用request.getParameter("userName")
检索参数的值:
cs.setString(1,request.getParameter("userName"));