日期:2014-05-18  浏览次数:20740 次

java 数据库开发的一些疑问
以下是一个向数据表插入数据的方法,执行完了以后,在关闭结果集时rs.close发生异常.
请问各位大侠,这怎么会发生异常呢!


  public boolean insertuser(UserInfo _us) throws SQLException
  {
  try{
  open();
  ps=con.prepareStatement("insert into userinfo (loginname,[password],sex,username,usertype,userclass) values(?,?,?,?,?,?)");
  ps.setString(1,_us.getLoginName());
  ps.setString(2,_us.getPassword());
  ps.setInt(3,_us.getSex());
  ps.setString(4,_us.getUserName());
  ps.setInt(5,_us.getUserType());
  ps.setString(6,_us.getUserClass());
  int a=ps.executeUpdate();
  if (a>0)
  return true;
  else 
  return false;
  }catch(SQLException ex)
  {  
  throw ex;
  }finally
  {
  rs.close();
  ps.close();
  con.close();
  }
  } 


异常记录如下:
java.lang.NullPointerException
at zhuce.DB.insertuser(DB.java:88)
at zhuce.registFrame.jButton1_actionPerformed(registFrame.java:162)
at zhuce.registFrame_jButton1_actionAdapter.actionPerformed(registFrame.java:180)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


------解决方案--------------------
支持自己搞定,提高得快!

executeUpdate 不返回rs, 如你所说,rs 根本没用到

建议你采用5楼的方式,单独做一个ConnectionUtil类来专门关闭数据库资源