- package dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import com.yourcompany.struts.form.BorrowForm;
- import common.DbConnection;
- public class BorrowDao
- {
-
-
- public int borrowBook(BorrowForm bf)
- {
-
-
-
- int a = 0;
-
- String sno = bf.getSno();
- String book_id = bf.getBook_id();
- String tname = bf.getTname();
- String id = bf.getId();
- Connection con = DbConnection.getConnection();
-
-
-
-
-
-
- long time=System.currentTimeMillis();
- long rtime=20*1000*60*60*24; //可借20天
- long returntime = time+rtime;
-
- SimpleDateFormat format1 =new SimpleDateFormat("yyyy-MM-dd");
- Date btime = new Date();
- String ndate = format1.format(btime);
- System.out.println(ndate);
-
-
- java.sql.Date obtime = java.sql.Date.valueOf(ndate);
-
- SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
- Date myDate = new Date(returntime);
- String returntime1 = format2.format(myDate);
- java.sql.Date returntime2= java.sql.Date.valueOf(returntime1);
- System.out.println(returntime1);
-
-
-
- int nownum=0,borrowed=0; //booksinfo表中的现有数量-1,借阅次数+1;
-
- boolean bool=true;
-
- try {
- PreparedStatement prst2;
- prst2 = con.prepareStatement("select nownum,borrowed from booksinfo where id=?"); [color=Red] //提示这行有错误!!!空指针异常.哪异常?[/color]
- prst2.setString(1, book_id);
- ResultSet rs2 = prst2.executeQuery();
-
- if(rs2.next())
- {
- nownum=rs2.getInt(1);
- borrowed=rs2.getInt(2);
- if(nownum<=0)
- { //防止图书借没了还能借出
- bool=false;
-
- }
- else
- {
- nownum--;
- borrowed++;
- }
- }
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
-
- if(bool)
- {
-
-
-
- try
- {
- PreparedStatement prst3;
- prst3 = con.prepareStatement("update booksinfo set nownum="+nownum+",borrowed="+borrowed+" where id='"+book_id+"'");
-
-
- PreparedStatement prst4 =
- con.prepareStatement
- ("insert into borrowinfo (id,sno,tname,book_id,borrowedtime,returntime) values('"+id+"','"+sno+"','"+tname+"','"+book_id+"','"+obtime+"','"+returntime2+"'");
- a = prst4.executeUpdate();
- con.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- }
-
-
-
-
- return a;
- }
- private SimpleDateFormat SimpleDateFormat(String string) {
- // TODO Auto-generated method stub
- return null;
- }
-
- }
复制代码 这是我做的图书管理系统中借书的Dao,但是网页提示是:
java.lang.NullPointerException
dao.BorrowDao.borrowBook(BorrowDao.java:63)
com.yourcompany.struts.action.BorrowAction.execute(BorrowAction.java:41)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
filter.CFilter.doFilter(CFilter.java:30)
我想问怎么就空指针异常了?我也没哪块是Null引用了啊.!!!!!!!!!!!!
愁死了,改一遍错一遍. |
|