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

时间:2014-03-20 08:57:23

标签: java sql

所以,我的代码片段是“没有执行SQL语句!

  

java.sql.SQLException:常规错误“

可能是由于这行 - “ResultSet rs = st.executeQuery(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+"%'  ";
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); 
}
}

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 !

2 个答案:

答案 0 :(得分:2)

您收到此错误是因为连接正在泄漏

使用后请务必关闭连接。

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

此外,在数据库上运行查询以确保它正常工作或将其更改为SELECT * FROM Section仅用于测试

答案 1 :(得分:2)

我遇到了类似的问题。我的观察是,如果preparedStatement抛出异常,则无法重用。抓住异常;关闭语句并重新创建它。 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对列有唯一约束。因此,当调用函数来编写&#34; WORD1&#34;第二次它会抛出约束违规的异常。 在catch块中,我刚刚写了一条消息。 现在当函数被调用为&#34; WORD2&#34;

失败
insert_eng_dist.setString(1, eng_word); 

有错误

java.sql.SQLException: statement is not executing

为了在catch块中处理这个问题,我关闭了语句并重新创建了它。这解决了问题。如下: -

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;
        }
    }