-1

NameまたはLatnameで検索しても、何も返されません。生年月日が指定されている場合にのみ値を返します。もう1つの質問は、生年月日を空白のままにすると 、「birthyear」列にnull値が表示されます。これは非null制約 エラーに違反します。

    import java.util.*;
    import java.sql.*;
    import java.io.*;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * Servlet implementation class searchfromdatabase
     */
    public class searchfromdatabase extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public searchfromdatabase() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            System.out.println("command: " + request.getParameter("command"));
            response.setContentType("text/html");
            java.io.PrintWriter out = response.getWriter();
            out.println("<html><head>");
            out.println("<title>Search form</title></head><body>");
            out.println("<h2>Artist details.</h2><tr>");

            // HTMl Customer Input
            out.println("<form method=\"post\" action =\""
                    + request.getContextPath() + "/searchfromdatabase\" >");

            out.println("<table border='0' width='300' align='center' bgcolor='#CDFFFF'>");
            out.println("<tr><td colspan=2 style='font-size:12pt;color:#00000;' align='center'><h3>Search Artist</h3></td></tr>");
            out.println("<tr><td ><b>Artist Name</b></td><td>: <input  type='text' name='givenname' id='givenname'></td></tr>");
            out.println("<tr><td ><b>Artist Last Name</b></td><td>: <input  type='text' name='familyname' id='familyname'></td></tr>");
            out.println("<tr><td ><b>Date of Birth</b></td><td>: <input  type='text' name='birthyear' id='birthyear'></td></tr>");
            out.println("<tr><td colspan=2 align='center'><input  type='submit' name='submit' value='Submit'></td></tr></table>");
            out.println("</form>");
            out.println("</body></html>");
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            java.io.PrintWriter out = response.getWriter();
            Connection conn = null;{
            try {
                Class.forName("org.postgresql.Driver");
                conn = DriverManager.getConnection(
                        "jdbc:postgresql://localhost:5432/caglar", "postgres",
                        "abracadabra");
                System.out.println("Connected to the database");
            String  agent_name  = request.getParameter("givenname");
            String  agent_lastname  = request.getParameter("familyname");
            String  dob  = request.getParameter("birthyear");

            //if dob is left blank
            if (request.getParameter("birthyear").equals("")) {
                dob = null;
                // otherwise, get the exact number and define it as variable
            } else {
                dob = request.getParameter("birthyear");
            }
            //
            ArrayList al=null;
            ArrayList agent_list =new ArrayList();

            //Problem 1: If dob is not given, it is not searching by name or lastname.

            String query = "select * from agent where givenname='"+agent_name+"' or familyname='"+agent_lastname+"' or birthyear='"+dob+"' order by givenname";
            System.out.println("query" + query);
            Statement st = conn.createStatement();
            ResultSet  rs = st.executeQuery(query);


            while(rs.next())
            {
                al  = new ArrayList();

              al.add(rs.getString(1));
              al.add(rs.getString(2));
              al.add(rs.getString(3));
              al.add(rs.getString(4));
              System.out.println("al :: "+al);
              agent_list.add(al);
            }

            request.setAttribute("agentList",agent_list);

         System.out.println("agentList " + agent_list);

        //   out.println("agent_list " + agent_list);




        // HTML results
        response.setContentType("text/html");
        //java.io.PrintWriter out = response.getWriter();
        out.println("<html><head>");
        out.println("<title>Final Results</title></head><body>");
        out.println("<table width='700px' align='center'  style='border:1px solid #000000;'><tr><td colspan=8 align='center' style='background-color:ffeeff'><b>Artist Record</b></td>");
        out.println("</tr><tr style='background-color:efefef;'><td><b>Artist ID</b></td><td><b>Date of Birth</b></td><td><b>Artist First Name</b></td><td><b>Artist Last Name</b></td></tr>");

        int count=0;
        String color = "#F9EBB3";


        if(request.getAttribute("agentList")!=null)
        {
         al=(ArrayList)request.getAttribute("agentList");
            Iterator itr = al.iterator();


            while(itr.hasNext())
            {

                if((count%2)==0)
                {

                    color = "#eeffee";


                }
                else
                {
                     color = "#F9EBB3";
                }
                count++;
                ArrayList agentList = (ArrayList)itr.next();

                out.println("<tr style='background-color:"+color+";'>");
                out.println("<td>"+agentList.get(0)+"</td>");
                out.println("<td>"+agentList.get(1)+"</td>");
                out.println("<td>"+agentList.get(2)+"</td>");
                out.println("<td>"+agentList.get(3)+"</td></tr>");
            }
        }
        if(count==0)
        {
            out.println("<tr><td colspan=8 align='center' style='background-color:eeffee'><b>No Record</b></td></tr>");
        }
        conn.close();
        System.out.println("Disconnected from database");
    } catch (Exception e) {
    e.printStackTrace();
    }
            out.println("</table></body></html>");  

        }

}

}
4

1 に答える 1

1
 String query = "select * from agent where givenname='"+agent_name+"' or familyname='"+agent_lastname+"' ";
if(dob!=null && !"".equals(dob))
  query = query + " or birthyear='"+dob+"'";

query = query+  "order by givenname";

クエリを作成するためのユーザー条件ステートメント。

于 2012-04-23T06:46:06.587 に答える