刚学jpetstore,遇到问题今天解决了

来源:互联网 发布:十字锈软件 编辑:程序博客网 时间:2024/04/29 16:43

做JPetStore不管怎么调试,总是一点注册按钮就会报错,空指针异常,所以查了一天,都不知道哪里出了问题,一直以后是impl类出现问题,然后去测试,发现sql语句没问题,然后又纠结在accountService.insert(account)到底正不正确,一直怀疑自己的思路不对,但是我不明白,明明参考源码里面,就是这样一个方法,为什么自己这样去写就会报错,气得我把doGet里面的语句全删了,只留下一个   System,out.print("username"),直接输入映射名看,发现也不正常,一直在报错,在浏览器输入localhost:8080/MyPetStore/registe?username=1234,但是控制台也获取不到值,不明白,为什么只留下一句也会错。没办法。

后来和一个同学研究,可能是我不小心删了桌面的mysql连接的驱动类,但是直接System.out也不至于会错啊,稀里糊涂的又下载了驱动类,引进去,发现,可以正常System,out.print了,唉,至今也不明白为什么。

但是一加上这句accountService.insert(account)依然不对,空指针百度了好久,知道是没有new某个东西,但是查来查去就是不知道,和小伙伴看了好久,晕,终于发现AccountService里没有不带参的构造方法,没有用accounDAO = new AccoutDAOImpl();对DAO进行初始化,希望在web学习的过程中更加注意和细心!

<span style="font-size:18px;">package org.csu.mypetstore.service;import org.csu.mypetstore.domain.Account;import org.csu.mypetstore.persistence.AccountDAO;import org.csu.mypetstore.persistence.impl.AccoutDAOImpl;public class AccountService {  private AccountDAO accounDAO;  public AccountService() {<span style="color:#ff0000;"></span></span><span style="font-size:24px;color:#ff0000;"><strong>accounDAO = new AccoutDAOImpl();//这里要特别注意初始化</strong></span><span style="font-size:18px;"><span style="color:#ff0000;"></span>}  public Account getAccount(String username) {    return accounDAO.getAccountByUsername(username);  }  public Account getAccount(String username, String password) {    Account account = new Account();    account.setUsername(username);    account.setPassword(password);    return accounDAO.getAccountByUsernameAndPassword(account);  }  public void insertAccount(Account account) {  System.out.println("abcde"); accounDAO.insertAccount(account); //accounDAO.insertProfile(account); //accounDAO.insertSignon(account);  }  public void updateAccount(Account account) { accounDAO.updateAccount(account); accounDAO.updateProfile(account);    if (account.getPassword() != null && account.getPassword().length() > 0) {    accounDAO.updateSignon(account);    }  }}</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">package org.csu.mypetstore.web.servlets;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.csu.mypetstore.domain.Account;import org.csu.mypetstore.service.AccountService;public class RegisteSevlet extends HttpServlet {<span style="white-space:pre"></span>public void doPost(HttpServletRequest request, HttpServletResponse response)<span style="white-space:pre"></span>throws ServletException, IOException {<span style="white-space:pre"></span>doGet(request, response);<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>public void doGet(HttpServletRequest request, HttpServletResponse response)<span style="white-space:pre"></span>throws ServletException, IOException { <span style="white-space:pre"></span>AccountService accountService=new AccountService();<span style="white-space:pre"></span>Account account=new Account();<span style="white-space:pre"></span>account.setUsername(request.getParameter("username"));<span style="white-space:pre"></span>account.setEmail(request.getParameter("email"));<span style="white-space:pre"></span>account.setFirstName(request.getParameter("firstName"));<span style="white-space:pre"></span>account.setLastName(request.getParameter("lastName"));<span style="white-space:pre"></span>//account.setStatus(request.getParameter("status"));<span style="white-space:pre"></span>account.setStatus("ok");<span style="white-space:pre"></span>account.setAddress1(request.getParameter("address1"));<span style="white-space:pre"></span>account.setAddress2(request.getParameter("address2"));<span style="white-space:pre"></span>account.setCity(request.getParameter("city"));<span style="white-space:pre"></span>account.setState(request.getParameter("state"));<span style="white-space:pre"></span>account.setZip(request.getParameter("zip"));<span style="white-space:pre"></span>account.setCountry(request.getParameter("country"));<span style="white-space:pre"></span>account.setPhone(request.getParameter("phone"));<span style="white-space:pre"></span>accountService.insertAccount(account);<span style="white-space:pre"></span><span style="white-space:pre"></span>}}</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">这是 AccountDAOImpl</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"><span style="white-space:pre"></span>public void insertAccount(Account account) {<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>    <span style="white-space:pre"></span> Connection conn = DBUtil.getConnection();<span style="white-space:pre"></span>            PreparedStatement pStatement = conn.prepareStatement(INSERT_ACCOUNT);<span style="white-space:pre"></span>            pStatement.setString(1, account.getEmail());<span style="white-space:pre"></span>            pStatement.setString(2, account.getFirstName());<span style="white-space:pre"></span>            pStatement.setString(3, account.getLastName());<span style="white-space:pre"></span>            pStatement.setString(4, account.getStatus());<span style="white-space:pre"></span>            pStatement.setString(5, account.getAddress1());<span style="white-space:pre"></span>            pStatement.setString(6, account.getAddress2());<span style="white-space:pre"></span>            pStatement.setString(7, account.getCity());<span style="white-space:pre"></span>            pStatement.setString(8, account.getState());<span style="white-space:pre"></span>            pStatement.setString(9, account.getZip());<span style="white-space:pre"></span>            pStatement.setString(10, account.getCountry());<span style="white-space:pre"></span>            pStatement.setString(11, account.getPhone());<span style="white-space:pre"></span>            pStatement.setString(12, account.getUsername());<span style="white-space:pre"></span>            int result = pStatement.executeUpdate();<span style="white-space:pre"></span>            <span style="white-space:pre"></span>            if (result==1) {<span style="white-space:pre"></span>System.out.print("success");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>            DBUtil.closePreparedStatement(pStatement);<span style="white-space:pre"></span>            DBUtil.closeConnection(conn);<span style="white-space:pre"></span>        } catch (Exception e) {<span style="white-space:pre"></span>            e.printStackTrace();<span style="white-space:pre"></span>        }</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>

0 0
原创粉丝点击