Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: SQL statement is not executed! java.sql.SQLException: General error

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SQL statement is not executed! java.sql.SQLException: General error

    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:-


     //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 THE OUTPUT:
    -----------------------
    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 ppz; March 20th, 2014 at 09:46 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,773
    Thanks
    54
    Thanked 2,340 Times in 2,311 Posts

    Default Re: SQL statement is not executed! java.sql.SQLException: General error

    1
    down vote
    From SO?
    Please post link to other sites this has been posted on.

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

  3. The Following User Says Thank You to Norm For This Useful Post:

    ppz (March 20th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: SQL statement is not executed! java.sql.SQLException: General error

    SO - I believe that's stackoverflow. This post appears to be a follow up of java - sqlq cannot be resolved to a variable - Stack Overflow.

    java.sql.SQLException: General error doesn't say much. From the output trace it seems that the SQLException was thrown while executing ResultSet rs = st.executeQuery(sqlq);. This corresponds to line 71 indicated by SearchBook.doPost(SearchBook.java:71) in the stack trace.

    There are a few things to check and try:

    1. Manually execute (via command line or any other facilities MS Access provides) the SQL Select that is constructed below against the database to confirm that the SQL statement is fine.
    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' ";

    2. The user and pass variables are empty strings. Is this intentional?

    3. Call the close() method for both the Statement and ResultSet objects once you have retrieved the result in the 1st try block. See Processing SQL Statements with JDBC (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics). (Note: This tutorial states, "When you are finished using a Statement, call the method Statement.close to immediately release the resources it is using. When you call this method, its ResultSet objects are closed." From my personal experience the automatic closing of the ResultSet object is dependent on the driver implementation. I know of at least 1 JDBC driver that doesn't close it as such, so it needs to be manually closed.)

    I do not think that non-closing of the Statement and ResultSet objects can cause the problem here, but close them anyway as not closing them can lead to memory leak later on.

    4. Remove Class.forName(driver); in the 2nd try block. The driver has already been loaded in the 1st try block, so there's no need to do it again.

    5. Remove con = DriverManager.getConnection(url, user, pass); in the 2nd try block. You can reuse the Connection object that was created earlier. In other words, the same Connection object can be used for multiple queries/updates.

    6. Add better SQLException handling code. Review Handling SQLExceptions (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics), and add handling code to get the SQLException's SQLState code, error code, cause, and reference to chained exceptions (if any). Model your code against the code provided in the tutorial.

    After you've done the above, if the problem persists, hopefully the enhanced SQLException handling code from #6 will inform us of the cause of the problem.

Similar Threads

  1. Replies: 1
    Last Post: July 20th, 2012, 07:08 PM
  2. [SOLVED] How to modify core Java interface java.sql.Statement.execute(String sql)?
    By amughost in forum Java Theory & Questions
    Replies: 6
    Last Post: June 9th, 2012, 05:31 PM
  3. Replies: 2
    Last Post: January 9th, 2012, 10:03 AM
  4. java.sql.SQLException: No data found
    By Virender in forum JDBC & Databases
    Replies: 2
    Last Post: December 7th, 2011, 01:17 PM
  5. Replies: 6
    Last Post: August 18th, 2010, 06:41 PM

Tags for this Thread