【问题标题】:Can someone tells me what is the error mean from Eclipse debug?有人可以告诉我 Eclipse 调试中的错误是什么意思吗?
【发布时间】:2017-06-10 14:00:06
【问题描述】:

我刚刚学会了通过 Eclipse 调试器浏览我的代码。 但是,我不知道为什么该过程一直返回到第 71 行 - ps.executeUpdate() 而控制台在第 79 行提到了错误,并且还提到了其他类的其他错误行。希望有人能告诉我这是怎么回事。

这是关于subjectDAOImpl的代码:

public void insertSubject(subject s) throws MyDataException { 
    try {       
        openConnection();               
        String qry = INSERT_QRY1; 
        //"INSERT INTO hi5project.subject(subject)VALUES (?)";              
        ps = connection.prepareStatement(qry);
        int i = 0;            
        String[] sub = new String[3];

        while(i < sub.length){              
            String e = s.getSubj(); 
            ps.setString(1, e); 
            ps.executeUpdate();        - line 71
        }

        if (ps !=null )
            closeConnection();

       }catch (SQLException e) {
            e.printStackTrace();
            throw new MyDataException("DB Error"); - line79
       }
   }

控制器:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    subject m = new subject();
    manager mgr = new manager();
    try {
        String[] sub = request.getParameterValues("subject");
        int i = 0;
        while (i < sub.length) {
            sub[i] = m.getSubj();
            mgr.insertSubject(m);
            out.println("Successful registered subject");
        }
    } catch (Exception ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    }
}

【问题讨论】:

  • 请将错误和代码显示为文本,而不是屏幕截图。
  • 我怀疑您在循环中执行准备好的语句的方式,我认为这可能会导致问题
  • @YCF_L,您能否更清楚地了解这个问题?我应该把 String[]sub 放在 try 之外吗?或...Sori..但我不清楚..
  • 检查我的答案@KarenGoh

标签: java mysql sql eclipse jdbc


【解决方案1】:

因为您正在执行多个语句,我建议改用语句批处理

我不知道你为什么要使用:

String[] sub = request.getParameterValues("subject");
int i = 0;
while (i < sub.length) {
    sub[i] = m.getSubj();
    mgr.insertSubject(m);
    out.println("Successful registered subject");
}

我认为你只需要使用:

String[] sub = request.getParameterValues("subject");
mgr.insertSubject(m);
out.println("Successful registered subject");

你可以把insertSubject的签名改成insertSubject(String[] sub),现在你可以用这批插入数据了:

connection.setAutoCommit(false);
try (PreparedStatement insert = connection.prepareStatement(qry)) {

    for (String s : sub) {//loop throw your array
        insert.setString(1, s);
        insert.addBatch();
    }
    insert.executeBatch();//executing the batch 
}
connection.commit();//commit statements to apply changes 

注意你的循环无法完成,因为你没有增加i,我已经改变了你的一些逻辑希望这可以帮助你,另一件事似乎是子数组总是空的所以你总是使用null

【讨论】:

  • 我的控制器我需要从我的 HTML 表单中读取值。所以,我仍然需要 getParameterValues....
  • @KarenGoh 是什么意思?
  • 我以为您正在为我的控制器显示正确的代码,然后我意识到它适用于其他类。顺便说一句,您不需要在 connection.commit() 之后 closeConnection() 吗?我必须添加 catch Exception 吗?
  • @KarenGoh 你必须用这段代码来代替while(i &lt; sub.length){ String e = s.getSubj(); ps.setString(1, e); ps.executeUpdate(); }
  • 是的。我现在知道了。但是,如何将 closeConnection() 和 catch Exception 添加到您的代码中?我试过了,但它不起作用
猜你喜欢
  • 2022-10-16
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2012-09-16
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多