Results 1 to 5 of 5
  1. #1
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default second query not getting executed

    My program basically has 2 executeQuery statements. Based on the result of the first sql query the 2nd sql query gets executed (I intend to use if else ladder). The control executes the first query but never executes the second one.
    my code:-
    ------------------
    Java Code:
     
      //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 sqlqueary="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(sqlqueary);
                    while (rs.next())
                    {
                    sqlResult = rs.getString(1);
    
                    }
                    System.out.println("Result retreived  1");
                    //System.out.println('"sqlqueary"');
                }
                catch (SQLException s)
                {
                    System.out.println("SQL statement is not executed! "+ s);
                }
            }
        catch (Exception e){
            e.printStackTrace();
        }
    System.out.println("************");
    //String sqlq="";
    
    if(sqlResult.equals("1"))
    {
        System.out.println("entered if block !");
    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 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");
    
                /*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)
            {
                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); 
    }
    }
    AND MY OUTPUT IS_
    ------------------------
    Connection created 1
    Result retreived 1
    ************
    entered if block !
    Connection created
    java.sql.SQLException: General error
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unkno wn Source)
    at SearchBook.doPost(SearchBook.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:723)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
    SQL statement is not executed in 2nd query! java.sql.SQLException: General error
    And it came here lastly !

    SO where is the error ?
    Last edited by ppreeti; 03-20-2014 at 02:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: second query not getting executed

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: second query not getting executed

    What String is passed to the executeQuery() method on line 71?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: second query not getting executed

    System.out.println your SQL statement so you can see exactly what is being run.
    Then take that statement and execute it in Access itself.

    I expect you'll find it doesn't work due to some typo or other.

    Also, the JDBC-ODBC bridge has not been supported for years (probably over 10 by now). Find another driver to use with Access. I think there are some out there these days (depending on the version of Access, I suspect).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: second query not getting executed

    Quote Originally Posted by Tolls View Post
    Also, the JDBC-ODBC bridge has not been supported for years (probably over 10 by now). Find another driver to use with Access. I think there are some out there these days (depending on the version of Access, I suspect).
    AFAIK - they're all commercial products.

    Java 8 is out now - is the ODBC-JDBC bridge driver removed as was promised in a blog post I wonder? I would check it out but I don't install a new release of Java until it hits update 5 ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 8
    Last Post: 08-23-2013, 05:49 AM
  2. Replies: 3
    Last Post: 10-19-2012, 10:16 PM
  3. My first java program could not be executed..help
    By miaaa00 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 08:07 AM
  4. Finally does not get executed
    By rahulkumar in forum New To Java
    Replies: 8
    Last Post: 04-06-2010, 05:30 PM
  5. Will finally get executed...
    By AlmostAGuru in forum New To Java
    Replies: 1
    Last Post: 08-10-2009, 07:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •