|
查看: 1248|回复: 3
|
java button控制数据更新database 问题
[复制链接]
|
|
|
小弟在写一个类似pos system 的java 小程式
可是遇到一个很怪的问题,尝试了很久还是无法解决
问题是这样的,当我要按个button 来把输入的数据update 进 mysql database 时,就会出现exception
private void jButton31ActionPerformed(ActionEvent evt) {
System.out.println("jButton31.actionPerformed, event=" + evt);
jComboBox1.addItem(jTextField1.getText());
try {
jTextField1.setText(null);
stmt = con.createStatement();
stmt.executeUpdate(sql1);
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(" rows were inserted " ;
}
}
结果出现 :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at As.Menu.jButton31ActionPerformed(Menu.java:314)
at As.Menu.access$5(Menu.java:301)
at As.Menu$6.actionPerformed(Menu.java:270)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
问题只有当存在这个eventperform 里面,如果我把 stmt = con.createStatement(); 和相关的几行去掉就没有问题。。。可是也就不能update 我的数据
我在网上找到一个跟我极相识的问题 http://www.experts-exchange.com/ ... ava/Q_21746419.html
各位高手如果知道问题所在请帮忙,因为真的很急
万分感谢~~!!!! |
|
|
|
|
|
|
|
|
|
|
发表于 17-3-2009 10:26 AM
|
显示全部楼层
|
我不是很会罢了,但是可以show一下你的sql statement吗? |
|
|
|
|
|
|
|
|
|
|
发表于 23-3-2009 11:34 PM
|
显示全部楼层
原帖由 wyewh 于 17-3-2009 08:00 AM 发表 
小弟在写一个类似pos system 的java 小程式
可是遇到一个很怪的问题,尝试了很久还是无法解决
问题是这样的,当我要按个button 来把输入的数据update 进 mysql database 时,就会出现exception
private void jB ...
你说把 stmt = con.createStatement(); 和相关的几行去掉就没有问题
那很可能是没有"con", con 是空 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-3-2009 11:47 PM
|
显示全部楼层
|
谢谢回复,已经做到了,的确如楼上说的,是con 或 stmt 是null |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|