Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 4.23 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. string search with two parameters
  2. package promotion;
  3.  
  4. import java.io.*;
  5.  
  6. import java.util.*;
  7.  
  8. import java.sql.*;
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12.  
  13.  
  14. public class PromotionServlet extends HttpServlet {
  15.     public PromotionServlet() {
  16.         super();
  17.     }
  18.  
  19.     private ServletConfig config;
  20.     String page = "PromotionResult.jsp";
  21.  
  22.     public void init(ServletConfig config) throws ServletException {
  23.         this.config = config;
  24.     }
  25.  
  26.     public void doGet(HttpServletRequest request,
  27.                       HttpServletResponse response) throws ServletException,
  28.                                                            IOException {
  29.         PrintWriter out = response.getWriter();
  30.         /*Establish connection to MySQL database*/
  31.  
  32.         String voucher_num = request.getParameter("voucher_num");
  33.         String nic = request.getParameter("nic");
  34.  
  35.  
  36.         String connectionURL =
  37.             "jdbc:mysql://localhost:3306/customer_promotion";
  38.         Connection connection = null;
  39.  
  40.         ResultSet rs;
  41.         response.setContentType("text/html");
  42.         //List dataList=new ArrayList();
  43.         String id = "error";
  44.         try {
  45.             Class.forName("com.mysql.jdbc.Driver");
  46.             connection = DriverManager.getConnection(connectionURL, "root", "mobitel#123");
  47.             Statement s = connection.createStatement();
  48.             rs =s.executeQuery("Select * from promotion_tbl where voucher_num='" + voucher_num + "' OR  nic='" + nic + "'");
  49.             String voucherNumber = "";
  50.             String nicNumber = "";
  51.             if (rs.next()) {
  52.                 rs.getString("voucher_num");
  53.                 rs.getString("nic");
  54.                 RequestDispatcher dispatcher = request.getRequestDispatcher(page);
  55.             } else {
  56.                 s.executeUpdate("INSERT INTO promotion_tbl VALUES('" +
  57.                                 voucher_num + "','" + nic + "')");
  58.                 System.out.println("Data successfully entered to the database");
  59.             }
  60.  
  61.  
  62.         } catch (Exception e) {
  63.  
  64.             System.out.println("Exception is ;" + e);
  65.             e.printStackTrace();
  66.  
  67.         }
  68.  
  69.         RequestDispatcher dispatcher = request.getRequestDispatcher(page);
  70.  
  71.         if (dispatcher != null) {
  72.  
  73.             dispatcher.forward(request, response);
  74.         }
  75.  
  76.     }
  77. }
  78.        
  79. <?xml version = '1.0' encoding = 'windows-1252'?>
  80. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  82.          version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
  83.   <servlet>
  84.     <servlet-name>PromotionServlet</servlet-name>
  85.     <servlet-class>promotion.PromotionServlet</servlet-class>
  86.   </servlet>
  87.   <servlet-mapping>
  88.     <servlet-name>PromotionServlet</servlet-name>
  89.     <url-pattern>/promotionservlet</url-pattern>
  90.   </servlet-mapping>
  91. </web-app>
  92.        
  93. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  94. "http://www.w3.org/TR/html4/loose.dtd">
  95. <%-- <%@ page contentType="text/html;charset=windows-1252"%>--%>
  96. <!--<html>
  97.   <head>
  98.     <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
  99.     <title>PromotionJSP</title>
  100.   </head>
  101.   <body></body>
  102. </html>-->
  103.  
  104. <%@ page contentType="text/html;charset=windows-1252"%>
  105. <%@page language="java" import="java.util.*" %>
  106. <html>
  107.  
  108.  
  109.        <head>
  110.        <title>Enter your Voucher Number and NIC number</title>
  111.        </head>
  112.       <body bgcolor="#999966">
  113.               <p>&nbsp;</p>
  114.               <form method="GET" action="PromotionServlet">
  115.  
  116.                      <p>
  117.                             <font color="#800000" size="5">
  118.                             <label for="Voucher_Number:">Enter Your Voucher Number </label></font>
  119.                             <input type="text" name="voucher_num" size="20"></input>
  120.                      </p>
  121.                      <p>
  122.                             <font color="#800000" size="5">
  123.                             <label for="NIC_Number:">Enter your NIC Number :</label></font>
  124.                             <input type="text" name="nic" size="20"></input>
  125.                      </p>
  126.                      <p>
  127.                             <input type="submit" value="Submit"></input>
  128.  
  129.                      </p>
  130.  
  131.               </form>
  132.        </body>
  133.  
  134. </html>