java - Like 搜索未获取所有匹配记录

标签 java sql jsp ms-access

我想做多表搜索。我在 MS Access 中有一个数据库,有 4 个表。所以我又创建了 1 个表 Index1,其中包含用户将搜索的所有 4 个表中的字段。(索引/主表的排序) )。问题是搜索没有显示理想情况下尽可能多的记录,它只显示其中的少数记录。请通过查找我的代码中的错误来启发我。提前致谢!

我的代码:

//package searchbook;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

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

public class SearchBook extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        HttpSession session = request.getSession(true);
        List booklist = new ArrayList();
        Connection con = null;

        String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\executive_db.accdb";

        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "";
        String pass = "";
        String category = "";
        category = request.getParameter("input");
        String sqlquery = "select   Index1.link_id "
                + "FROM Index1 "
                + " WHERE  Index1.index_name LIKE '%" + category + "%'  ";
        String sqlResult = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, pass);
            try {
                Statement st = con.createStatement();
                System.out.println("Connection created 1");
                ResultSet rs = st.executeQuery(sqlquery);
                while (rs.next()) {
                    sqlResult = rs.getString(1);

                }
                System.out.println("Result retreived  1");
                //System.out.println('"sqlquery"');
            } catch (SQLException s) {
                System.out.println("SQL statement is not executed! " + s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("************");
        String sqlq = "";

        int flag = 0;


    /*if(sqlResult.equals("1"))
    {
        flag=1;
        System.out.println("entered if block for section!");
        sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM  Report "
                + " where Report.Report_Name LIKE '%"+category+"%' ";
         sqlq="select  Section.Section_Name ,  Report.Report_Name , Report.Link, Contact.Contact_Name "
                + "FROM Section , Report , Contact"
                + " WHERE  Report.Section_ID=Section.Section_ID  and  Contact.Contact_ID=Report.Report_ID "
                + "and  Section.Section_Name = '"+category+"' ";
    }   */
        if (sqlResult.equals("1")) {
            flag = 1;
            System.out.println("entered if block for section!");
        /*sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM  Report "
                + " where Report.Report_Name LIKE '%"+category+"%' ";*/
            sqlq = "select distinct Section.Section_Name ,  Report.Report_Name , Report.Link, Contact.Contact_Name "
                    + "FROM Section , Report , Contact"
                    + " WHERE  Report.Section_ID=Section.Section_ID  and  Contact.Contact_ID=Report.Contact_ID and  Section.Section_Name LIKE '%" + category + "%' ";
            // + "and  Section.Section_Name = '"+category+"' ";
        }

        if (sqlResult.equals("2")) {
            flag = 1;
            System.out.println("entered if block for report!");
        /*sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM  Report "
                + " where Report.Report_Name LIKE '%"+category+"%' ";*/
            sqlq = "select distinct Section.Section_Name ,  Report.Report_Name , Report.Link, Contact.Contact_Name "
                    + "FROM Section , Report , Contact"
                    + " WHERE  Report.Section_ID=Section.Section_ID  and  Contact.Contact_ID=Report.Contact_ID and  Report.Report_Name LIKE '%" + category + "%' ";
            //   + "and  Report.Report_Name = '"+category+"' ";
        }
        if (sqlResult.equals("3")) {
            flag = 1;
            System.out.println("entered if block for metrics !");
        /*sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM  Report "
                + " where Report.Report_Name LIKE '%"+category+"%' "*/
            ;

            sqlq = "select distinct Section.Section_Name ,  Report.Report_Name , Report.Link, Contact.Contact_Name "
                    + "FROM Section , Report , Contact,Metrics"
                    + " WHERE  Report.Section_ID=Section.Section_ID  and  Contact.Contact_ID=Report.Contact_ID and Metrics.Report_ID=Report.Report_ID "
                    + "and  Metrics.Metric_Name LIKE '%" + category + "%' ";
        }
        if (sqlResult.equals("4")) {
            flag = 1;
            System.out.println("entered if block for contact name!");
        /*sqlq="select Section.Section_Name ,  Report.Report_Name , Report.Link, Contact.Contact_Name, Metrics.Metric_Name "
                  + "FROM Section , Report , Contact, Metrics"
                  + " WHERE  Report.Section_ID=Section.Section_ID  and  Metrics.Report_ID=Report.Report_ID "
                  + "and Report.Report_ID IN (SELECT Report.Report_ID FROM Report  WHERE "
                  + "Contact.Contact_ID=Report.Contact_ID and Contact.Contact_Name LIKE '%"+category+"%' and Metrics.Metric_Segment = 'M') ORDER BY Report_Name ";*/


            sqlq = "select distinct Section.Section_Name ,  Report.Report_Name , Report.Link, Contact.Contact_Name "
                    + "FROM Section , Report , Contact"
                    + " WHERE  Report.Section_ID=Section.Section_ID  and  Contact.Contact_ID=Report.Contact_ID "
                    + "and  Contact.Contact_Name LIKE '%" + category + "%' ";
        }


        if (flag == 1) {
            try {
                Class.forName(driver);
                con = DriverManager.getConnection(url, user, pass);
                try {
                    Statement st = con.createStatement();
                    System.out.println("Connection created");
                    ResultSet rs = st.executeQuery(sqlq);
                    System.out.println("Result retreived for 2nd query ");
                    while (rs.next()) {
                        List<String> book = new ArrayList<String>();

                        String Name = rs.getString("Section_Name");
                        String reportName = rs.getString("Report_Name");
                        String link = rs.getString("Link");
                        String contactName = rs.getString("Contact_Name");
               /* String metricName=rs.getString("Metric_Name");*/
                        //String reportId=rs.getString("Report_ID");

                        book.add(Name);
                        book.add(reportName);
                        book.add(link);
                        book.add(contactName);
               /* book.add(metricName);*/
                        //book.add(reportId);

              /*  book.add(ind_id);
                book.add(ind_name);*/

                        booklist.add(book);
                    }
                } catch (SQLException s) {
                    s.printStackTrace();
                    System.out.println("SQL statement is not executed in 2nd query! " + s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("And it came here lastly !");
        request.setAttribute("booklist", booklist);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/searchbook.jsp");
        dispatcher.forward(request, response);
        System.out.println("***************************************************************************************");
    }
}

最佳答案

再检查一下你的逻辑。在这个循环中

while (rs.next()) {
    sqlResult = rs.getString(1);

}

您正在循环第一个结果集,重复分配

sqlResult = rs.getString(1); 

然后什么也不做,直到到达 while 循环的末尾,之后您继续对第一个查询获取的最后一行执行一些其他操作。

关于java - Like 搜索未获取所有匹配记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130955/

相关文章:

java - 非 JSP(独立)上下文中的 JSTL/JSP EL(表达式语言)

java - 将 http post 发送到 dot net webapi

Java 正则表达式检查包含网址的字符串

java - Flutter 不是只创建 Java 类,而是 Kotlin

sql - 从SQLite中的特定日期检索数据

java - 使用 HttpServletRequestWrapper 时无法发送 jsp 参数?

java - 在JSP中显示当前页面的表单处理结果

java - 检查 2 个 BST 的值是否相等

mysql - mysql 中可以在不覆盖的情况下联合吗?

mysql - 在不更改数据库或 SQL 查询的情况下,MySQL 上的 SQL 查询多花三秒