Java 机智” +“其中Report.Section\u ID=Section.Section\u ID和Contact.Contact\u ID=Report.Contact\u ID” +与Contact.Contact_名称类似“%”+category+“%”; } 如果(标志==1){ 试一试{ Class.forName(驱动程序); con=DriverManager.getConnection(url、用户、通行证); 试一试{ 语句st=con.createStatement(); System.out.println(“已创建连接”); 结果集rs=st.executeQuery(sqlq); System.out.println(“为第二次查询检索的结果”); while(rs.next()){ List book=new ArrayList(); String Name=rs.getString(“Section_Name”); String reportName=rs.getString(“报告名称”); 字符串链接=rs.getString(“链接”); String contactName=rs.getString(“Contact_Name”); /*String metricName=rs.getString(“Metric_Name”)*/ //String reportId=rs.getString(“Report_ID”); 书。加(名); 增加(报告名称); book.add(link); book.add(联系人姓名); /*book.add(metricName)*/ //book.add(reportId); /*图书编号(ind_id); 书。加(姓名)*/ 图书目录。添加(图书); } }捕获(SQLS异常){ s、 printStackTrace(); System.out.println(“第二次查询中未执行SQL语句!”+s); } }捕获(例外e){ e、 printStackTrace(); } } System.out.println(“它最后来到这里了!”); request.setAttribute(“booklist”,booklist); RequestDispatcher=getServletContext().getRequestDispatcher(“/searchbook.jsp”); 转发(请求、响应); System.out.println(“**********************************************************************************************************************************************************************************************************”); } }
访问的工作原理不同。对于类似的操作,请使用“*”而不是“%”。再次检查您的逻辑。在此循环中Java 机智” +“其中Report.Section\u ID=Section.Section\u ID和Contact.Contact\u ID=Report.Contact\u ID” +与Contact.Contact_名称类似“%”+category+“%”; } 如果(标志==1){ 试一试{ Class.forName(驱动程序); con=DriverManager.getConnection(url、用户、通行证); 试一试{ 语句st=con.createStatement(); System.out.println(“已创建连接”); 结果集rs=st.executeQuery(sqlq); System.out.println(“为第二次查询检索的结果”); while(rs.next()){ List book=new ArrayList(); String Name=rs.getString(“Section_Name”); String reportName=rs.getString(“报告名称”); 字符串链接=rs.getString(“链接”); String contactName=rs.getString(“Contact_Name”); /*String metricName=rs.getString(“Metric_Name”)*/ //String reportId=rs.getString(“Report_ID”); 书。加(名); 增加(报告名称); book.add(link); book.add(联系人姓名); /*book.add(metricName)*/ //book.add(reportId); /*图书编号(ind_id); 书。加(姓名)*/ 图书目录。添加(图书); } }捕获(SQLS异常){ s、 printStackTrace(); System.out.println(“第二次查询中未执行SQL语句!”+s); } }捕获(例外e){ e、 printStackTrace(); } } System.out.println(“它最后来到这里了!”); request.setAttribute(“booklist”,booklist); RequestDispatcher=getServletContext().getRequestDispatcher(“/searchbook.jsp”); 转发(请求、响应); System.out.println(“**********************************************************************************************************************************************************************************************************”); } },java,sql,jsp,ms-access,Java,Sql,Jsp,Ms Access,访问的工作原理不同。对于类似的操作,请使用“*”而不是“%”。再次检查您的逻辑。在此循环中 while(rs.next()){ sqlResult=rs.getString(1); } 您正在循环第一个结果集,重复分配 sqlResult=rs.getString(1); 然后什么也不做,直到到达while循环的末尾,然后继续对第一个查询获取的最后一行执行其他操作。在执行之前打印查询,当我查询dig(部分字符串搜索)时,理想情况下,它应该检索包含数字多活动、数字关闭、数字活动、数字仪表板数据
while(rs.next()){
sqlResult=rs.getString(1);
}
您正在循环第一个结果集,重复分配
sqlResult=rs.getString(1);
然后什么也不做,直到到达
while
循环的末尾,然后继续对第一个查询获取的最后一行执行其他操作。在执行之前打印查询,当我查询dig(部分字符串搜索)时,理想情况下,它应该检索包含数字多活动、数字关闭、数字活动、数字仪表板数据透视表、数字采集等的所有记录,这些记录存在于不同的表中,因此它应该输入多个表并获取记录,但它不这样做。在这种情况下,它只输入一个表并获取记录之所以选择MySQL标记,是因为问题明确与Access有关。在本例中不是。对于从Microsoft Access应用程序本身外部查询ACE/Jet(“Access”)数据库,请使用百分号(%
)是要使用的正确通配符。我必须对此进行检查…MSDN说明如下:星号仅应在通过DAO执行或直接从Microsoft Access执行时用作通配符。在使用ADO或ADO.NET时,应使用百分比符号。这不是ODBC或OLEDB,因此我不确定。实际上,它是ODBC,通过Java的JDBC-ODBC桥接器。@Gord-Ah!抱歉。我的技能不是Java,而是SQL。我想我能帮上忙。祝你好运。也许你是对的。那么我应该如何更正它?我应该在所有if语句之前添加一个while循环吗?实际上,循环必须在那里发生,因为多个表都有用户可能正在搜索的匹配字符串。应该怎么做完成。@user3444944将代码放在第一个while
循环中搜索各个表,以便对“索引表”的初始查询返回的每个值执行它。请告诉我怎么做,我对此有点困惑。谢谢!
//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("***************************************************************************************");
}
}