gpt4 book ai didi

java - sqlq 无法解析为变量

转载 作者:行者123 更新时间:2023-12-01 13:15:13 25 4
gpt4 key购买 nike

我在下面程序的“ResultSet rs = st.executeQuery(sqlq)”行中的 Eclipse 中收到错误“sqlq 无法解析为变量”。请帮助我解决同样的问题。提前致谢!

//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+"%' ";
try
{
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
try{
Statement st = con.createStatement();
System.out.println("Connection created");
ResultSet rs = st.executeQuery(sqlquery);
System.out.println("Result retreived ");
}
catch (SQLException s)
{
System.out.println("SQL statement is not executed! "+ s);
}
}
catch (Exception e){
e.printStackTrace();
}
if(sqlquery.equals("1"))
{
String sqlq="select Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name, Metrics.Metric_Name "
+ "FROM Section , Report , Contact,Metrics "
+ "WHERE Report.Contact_ID=Contact.Contact_ID and Report.Section_ID=Section.Section_ID "
+ "and Report.Report_ID IN (SELECT Metrics.Report_ID FROM Metrics WHERE Metrics.Metric_Name = Report.Report_ID') and Metrics.Metric_Segment = 'M' ";
}

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

/*String ind_id=rs.getString("index_name");
String ind_name=rs.getString("link_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)
{
System.out.println("SQL statement is not executed! "+ s);
}
}
catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("booklist",booklist);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/searchbook.jsp");
dispatcher.forward(request, response);
}
}

最佳答案

您在 if 范围内声明 sqlq,因此当您离开该 block (由 {} 分隔)时,它不会可访问。

您可以尝试在 if block 之外定义 sqlq:

String sqlq = "";

if (...) {
sqlq = ...;
}

关于java - sqlq 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com