2

So, my piece of code is giving "SQL statement is not executed!

java.sql.SQLException: General error"

Probably it is due to this line-"ResultSet rs = st.executeQuery(sqlq);" in my code. What could be the possible reason to it and how can it be corrected ? please help !

//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="";

if(sqlResult.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' ";

System.out.println("2nd query executed too !");
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();
}}
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 in Eclipse console as:

   Connection created 1 Result retreived 1 ************ 2nd query executed too ! Connection created SQL statement is not executed! java.sql.SQLException:: General error And it came here lastly !
3
  • no stacktrace but this is what appears in my console window "Connection created 1 Result retreived 1 ************ 2nd query executed too ! Connection created SQL statement is not executed! java.sql.SQLException: General error And it came here lastly ! " Thought it could be of help ! Commented Mar 20, 2014 at 9:01
  • try to print the stack trace in the catch block which u have written inside you are having an exception there.,also print you query and its result in sysout. Commented Mar 20, 2014 at 9:04
  • Why Loading the Driver Class 2 times when loading it once is sufficient , Same goes with Connection Commented Feb 9, 2015 at 17:49

2 Answers 2

3

You are getting this error because the connection is leaking

Always close the connection after using it.

Connection con = null;
try{
  //....
  // init con
} catch(SQLException se) {
   se.printStackTrace();
} finally {
   try {
      if (con!=null) con.close();
   } catch (SQLException se) {
      se.printStackTrace();
   }
}

Also, run the query on the database to make sure it is working or change it to SELECT * FROM Section just for testing

Sign up to request clarification or add additional context in comments.

Comments

2

I faced similar problem. My observation is that if preparedStatement has thrown an exception then it can not reused. So catch exception; close the statement and recreate it. e.g.

static PreparedStatement insert_eng_dist; 
public static void main(){
    // JDBC connection related code
    // Connection c = ....  
    insert_eng_dist = c.prepareStatement("insert into DIST_ENG_WORD values(?)");
    insertIntoDistinctTable("WORD1");
    insertIntoDistinctTable("WORD1");
    insertIntoDistinctTable("WORD2");
}

public static void insertIntoDistinctTable(String eng_word) throws Exception{
    try{
        insert_eng_dist.setString(1, eng_word); 
        insert_eng_dist.execute();
    }catch(SQLException e){
        if(e.getMessage().equals("[SQLITE_CONSTRAINT]  Abort due to constraint violation (column ENG_WORD is not unique)")){
            System.out.println("Word already exists in distinct table so not adding again");
        }else {
            throw e;
        }
    }

DIST_ENG_WORD has unique constraint on column. So when function is called to write "WORD1"second time it throws exception for constraint violation. In catch block I have just written message. Now when function is called to for "WORD2" it fails in

insert_eng_dist.setString(1, eng_word); 

with error

java.sql.SQLException: statement is not executing

To handle this in catch block I closed the statement and recreated it. This solved problem. As follows :-

public static void insertIntoDistinctTable(String eng_word) throws Exception{
    try{
        insert_eng_dist.setString(1, eng_word); 
        insert_eng_dist.execute();
    }catch(SQLException e){
        if(e.getMessage().equals("[SQLITE_CONSTRAINT]  Abort due to constraint violation (column ENG_WORD is not unique)")){
                System.out.println("Word already exists in distinct table so not adding again");
                insert_eng_dist.close();
                insert_eng_dist = c.prepareStatement("insert into DIST_ENG_WORD values(?)");
        }else {
            throw e;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.