使用Java Servlet从PostgreSQL中搜索。无法获得任何结果显示

时间:2012-04-22 04:28:26

标签: java postgresql servlets

当我按名称或Latname搜索时,它不会返回任何内容。只有在给出出生日期时才返回值。另一个问题是,如果出生日期是空白我得到     列“birthyear”中的空值违反了非空约束错误。

    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>");  

        }

}

}

1 个答案:

答案 0 :(得分: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";

用于构造查询的用户条件语句。