【问题标题】:Data Access object method not working数据访问对象方法不起作用
【发布时间】:2017-05-16 21:13:45
【问题描述】:

我是一名学习 JSP 的学生,我在通过 DAO 类的对象执行方法时似乎遇到了这个问题。当在 servlet 本身上给出数据库连接和 SQL 查询时,它将起作用。但是当在 DAO 类中给出并使用对象时,它不起作用。请帮忙。

import dataaccessobjects.cartDAO1;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class addtoCartServ extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    cartDAO1 newcart = new cartDAO1();
    PrintWriter out = response.getWriter();

    if (request.getParameter("submit") != null){

                //out.println("added to cart");
             try {
            //out.println("submit not null");
            String Uname = (String) request.getSession().getAttribute("Welcome");
            String ino = request.getParameter("ino");
            String iqnty = request.getParameter("quantity");
            String iname = request.getParameter("iname");

          if(newcart.addToCart(iname,Uname,ino,iqnty)){

          out.println("added to cart");
          }
            } catch (SQLException ex) {
            Logger.getLogger(addtoCartServ.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(addtoCartServ.class.getName()).log(Level.SEVERE, null, ex);
        }


        }
}

}

DAO 类

public cartDAO1(){
}
public boolean addToCart(String iname,String username, String ino,String       iqnty) throws SQLException, ClassNotFoundException{
 boolean flag = false;

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn =       DriverManager.getConnection("jdbc:mysql://localhost:3306/styleomega","root","");
        PreparedStatement ps = conn.prepareStatement("INSERT INTO   cart(iname,uname,ino,iqnty) VALUES (?,?,?,?)");


        // set the values for parameters
            ps.setString(1,iname);
           ps.setString(2,username);
            ps.setString(3,ino);
            ps.setString(4,iqnty);
            int rs = ps.executeUpdate();

        if (rs==1){
        flag = true;
        }
        return flag;


} 

}

【问题讨论】:

  • 是有错误还是没有结果?
  • 我不明白到底什么不起作用?但我注意到,您没有关闭 DAO 类中的数据库连接。

标签: java mysql jsp servlets dao


【解决方案1】:

您应该在 servlet 中导入 DAO 类包,然后访问它会像这样工作

import DAO.cartDao; 

如果你不导入,那么如何访问它

【讨论】:

  • 对不起,我没有在问题中添加它们。但是我确实导入了包和类。我现在已经编辑了问题。
  • 那么执行时会出现什么错误或警告
【解决方案2】:

我不明白究竟是什么不工作?但我注意到,您没有关闭 DAO 类中的语句和数据库连接。

仅供参考:一个例子

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
 conn = // Retrieve connection
 stmt = conn.prepareStatement(// Some SQL);
 rs = stmt.executeQuery();
} catch(Exception e) {
  // Error Handling
} finally {
 try { if (rs != null) rs.close(); } catch (Exception e) {};
 try { if (stmt != null) stmt.close(); } catch (Exception e) {};
 try { if (conn != null) conn.close(); } catch (Exception e) {};
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多