简体   繁体   English

SQL语句未执行! java.sql.SQLException:一般错误

[英]SQL statement is not executed! java.sql.SQLException: General error

So, my piece of code is giving "SQL statement is not executed! 因此,我的一段代码给出了“ SQL语句未执行!

java.sql.SQLException: General error" java.sql.SQLException:常规错误”

Probably it is due to this line-"ResultSet rs = st.executeQuery(sqlq);" 可能是由于这一行-“ 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: Eclipse控制台中的输出为:

   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 !

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 另外,在数据库上运行查询以确保其正常运行,或者将其更改为SELECT * FROM Section仅用于测试

I faced similar problem. 我遇到了类似的问题。 My observation is that if preparedStatement has thrown an exception then it can not reused. 我的观察是,如果prepareStatement引发了异常,那么它将无法重用。 So catch exception; 因此捕获异常; close the statement and recreate it. 关闭该语句并重新创建它。 eg 例如

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. DIST_ENG_WORD对列具有唯一约束。 So when function is called to write "WORD1"second time it throws exception for constraint violation. 因此,当第二次调用函数写入“ WORD1”时,它将引发约束冲突的异常。 In catch block I have just written message. 在catch块中,我刚刚写了一条消息。 Now when function is called to for "WORD2" it fails in 现在,当为“ WORD2”调用函数时,它将失败

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. 为了在catch块中处理此问题,我关闭了该语句并重新创建了它。 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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM