Editing Table Doesnt Wanna Work! :(
this is my method that is used to edit contacts in my CONTACTS table:publicvoid editContact(ContactPanel updatePanel){
try{
preparedStatement = connection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
preparedStatement.setString(1, updatePanel.getName());
preparedStatement.setString(2, updatePanel.getNumber());
preparedStatement.setString(3, updatePanel.getEMail());
preparedStatement.setInt(4, updatePanel.getID());
preparedStatement.executeUpdate();
}catch(SQLException ex){
ex.printStackTrace();
}
}
i checked what execute update returned, and it returned 1. this means that a row was in fact updated but for some reason my table just doesnt wanna update! maybe u guys could help me out here? thnx :)
[1140 byte] By [
Alex1989a] at [2007-11-27 10:35:21]

# 1
I'm not sure about all you values but this should work.
public void editContact ()
{
try
{
Statement s = conn.createStatement ();
String preparedStatement = "UPDATE tblContact ";
String Name = Name.getText ();
String Numbr = Number.getText ();
String EMail = EMail.getText ();
String ID = ID.getText ();
preparedStatement += "SET NAME = '" + Name;
preparedStatement += "' SET NUMBER = '" + Numbr;
preparedStatement += "' SET EMAIL = '" + EMail;
preparedStatement += "WHERE ID = " + ID ();
s.executeUpdate (preparedStatement);
}
catch (SQLException ex)
{
ex.printStackTrace ();
}
}
Also don 't forget that if the database data type is set to you don' t need the ' around the new values.
regards Paul
# 2
But if you gatta have a : "preparedStatement" try this
public void editContact ()
{
try
{
// as far as i know connection. should be con. ?
preparedStatement pstmt = connection.prepareStatement ("UPDATE CONTACTS SET NAME = ?, NUMBER = ?, EMAIL = ? WHERE ID = ?");
pstmt.setString (1, updatePanel.getName ());
pstmt.setString (2, updatePanel.getNumber ());
pstmt.setString (3, updatePanel.getEMail ());
pstmt.setInt (4, updatePanel.getID ());
pstmt.executeUpdate();
}
catch (SQLException ex)
{
ex.printStackTrace ();
}
}
Hope those help :)
null
# 3
> But if you gatta have a : "preparedStatement" try
> this
> > public void editContact ()
> {
>try
> {
> // as far as i know connection. should be con.
> ?
> preparedStatement pstmt =
> connection.prepareStatement ("UPDATE CONTACTS SET
> NAME = ?, NUMBER = ?, EMAIL = ? WHERE ID = ?");
>
> pstmt.setString (1, updatePanel.getName ());
> pstmt.setString (2, updatePanel.getNumber ());
> pstmt.setString (3, updatePanel.getEMail
> ());
>pstmt.setInt (4, updatePanel.getID ());
>pstmt.executeUpdate();
> }
>catch (SQLException ex)
> {
>ex.printStackTrace ();
> }
>
>
> Hope those help :)
>
> null
How is that different from the OP?
Unless your connection is auto-committing you're missing aconnection.commit();
at the end.
ps. always use prepared statements, it's not a matter of "if you gatta have".
dwga at 2007-7-28 18:34:31 >

# 4
ok well i changed a few things to try and sort the problem out but might have found out why the contacts arent getting updated: public void editContact(ContactPanel updatePanel){
try{
editStatement = editConnection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
editStatement.setString(1, updatePanel.getName());
editStatement.setString(2, updatePanel.getNumber());
editStatement.setString(3, updatePanel.getEMail());
editStatement.setInt(4, updatePanel.getID());
editStatement.executeUpdate();
}catch(SQLException ex){
ex.printStackTrace();
}
}
i made a separate preparedStatement (editStatement) and a separate connection (editConnection) instead of using the prepared statement and connection from my save and load methods, and found out that i get a null pointer exception: Exception occurred during event dispatching:
java.lang.NullPointerException
at JavaPhonebook.ContactIO.editContact(ContactIO.java:102)
at JavaPhonebook.ContactPanel.actionPerformed(ContactPanel.java:335)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:177)
at java.awt.Dialog$1.run(Dialog.java:1039)
at java.awt.Dialog$3.run(Dialog.java:1091)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1089)
at java.awt.Component.show(Component.java:1419)
at java.awt.Component.setVisible(Component.java:1372)
at java.awt.Window.setVisible(Window.java:801)
at java.awt.Dialog.setVisible(Dialog.java:979)
at JavaPhonebook.ContactPanel.buildEditDialog(ContactPanel.java:284)
at JavaPhonebook.ContactPanel.actionPerformed(ContactPanel.java:328)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
# 5
That NullPointerException has nothing to do with connections or statements, it occurs at line 102 in your ContactIO class, presumably that line looks something likecontactIO.editContact(contactPanel);
Most likely contactIO is null.
As I said previously, the original problem is most likely because of a missingconnection.commit();
There should be no need for a different connection for updates.
dwga at 2007-7-28 18:34:31 >

# 6
> That NullPointerException has nothing to do with
> connections or statements, it occurs at line 102 in
> your ContactIO class, presumably that line looks
> something
> likecontactIO.editContact(contactPanel);
M
> ost likely contactIO is null.
line 102 in my ContactIO class is editStatement = editConnection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
haha :)
> As I said previously, the original problem is most
> likely because of a
> missingconnection.commit();
There should
> be no need for a different connection for updates.
the editConnection.commit();
didnt seem work no matter where i put it in the method :(
# 7
> line 102 in my ContactIO class is editStatement
> = editConnection.prepareStatement("update CONTACTS
> set NAME = ?, NUMBER = ?, EMAIL = ? where ID =
> ?");
haha :)
OK, then editConnection is null at that line.
> the editConnection.commit();
didnt seem
> work no matter where i put it in the method :(
You should put it after you executeUpdate and before you close the connection.
Edit:
Try the followingpublic void editContact(ContactPanel updatePanel){
try{
preparedStatement = connection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
preparedStatement.setString(1, updatePanel.getName());
preparedStatement.setString(2, updatePanel.getNumber());
preparedStatement.setString(3, updatePanel.getEMail());
preparedStatement.setInt(4, updatePanel.getID());
preparedStatement.executeUpdate();
connection.commit();
}catch(SQLException ex){
ex.printStackTrace();
}
}
Adding of course the necessary close statements.
Message was edited by:
dwg
dwga at 2007-7-28 18:34:31 >

# 8
that still doesnt work :( how can editStatement = editConnection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
be null? my new method looks like this: public void editContact(ContactPanel updatePanel){
try{
editStatement = editConnection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
editStatement.setString(1, updatePanel.getName());
editStatement.setString(2, updatePanel.getNumber());
editStatement.setString(3, updatePanel.getEMail());
editStatement.setInt(4, updatePanel.getID());
editStatement.executeUpdate();
editConnection.commit();
}catch(SQLException ex){
ex.printStackTrace();
}finally{
try{
connection.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
# 9
> that still doesnt work :( how can editStatement
> = editConnection.prepareStatement("update CONTACTS
> set NAME = ?, NUMBER = ?, EMAIL = ? where ID =
> ?");
be null?
I didn't say the entire statement was null, only the variable editConnection.
Add System.out.println(editConnection == null); just before the trouble line, if it prints "null" then editConnection is null, and that would be because you haven't initialized it.
> my new method looks like this:
> public void editContact(ContactPanel
> updatePanel){
> try{
> editStatement =
> editConnection.prepareStatement("update CONTACTS set
> NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
> editStatement.setString(1,
> updatePanel.getName());
> editStatement.setString(2,
> updatePanel.getNumber());
> editStatement.setString(3,
> updatePanel.getEMail());
> editStatement.setInt(4,
> updatePanel.getID());
>editStatement.executeUpdate();
> editConnection.commit();
>}catch(SQLException ex){
>ex.printStackTrace();
>}finally{
> try{
>connection.close();
>}catch(SQLException ex){
>ex.printStackTrace();
> }
>}
>
First of all you are calling close() on connection, not on editConnection, that's not right. Secondly, you need to call close() on the statement as well (editStatement that is).
Try this:public void editContact(ContactPanel updatePanel) {
try {
editStatement = editConnection.prepareStatement("update CONTACTS set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
editStatement.setString(1, updatePanel.getName());
editStatement.setString(2, updatePanel.getNumber());
editStatement.setString(3, updatePanel.getEMail());
editStatement.setInt(4, updatePanel.getID());
System.out.println(updatePanel.getName() + ", " + updatePanel.getNumber() + ", " +
updatePanel.getEMail() + ", " + updatePanel.getID());
int count = editStatement.executeUpdate();
System.out.println("updated rows = " + count);
editConnection.commit();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
editStatement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
editConnection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
And tell me the output.
dwga at 2007-7-28 18:34:31 >

# 10
> Try this:public void editContact(ContactPanel
> updatePanel) {
> try {
> editStatement =
> = editConnection.prepareStatement("update CONTACTS
> set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");
> editStatement.setString(1, updatePanel.getName());
> editStatement.setString(2,
> 2, updatePanel.getNumber());
> editStatement.setString(3,
> 3, updatePanel.getEMail());
> editStatement.setInt(4, updatePanel.getID());
> System.out.println(updatePanel.getName() + ", " +
> + updatePanel.getNumber() + ", " +
> updatePanel.getEMail() + ", " +
> " + updatePanel.getID());
> int count = editStatement.executeUpdate();
> System.out.println("updated rows = " + count);
> editConnection.commit();
> } catch (SQLException ex) {
> ex.printStackTrace();
> } finally {
> try {
> editStatement.close();
> } catch (SQLException ex) {
> ex.printStackTrace();
> }
> try {
> editConnection.close();
> } catch (SQLException ex) {
> ex.printStackTrace();
> }
> }
> }
And tell me the output.
this is my output from that: Exception occurred during event dispatching:
java.lang.NullPointerException
at JavaPhonebook.ContactIO.editContact(ContactIO.java:128)
at JavaPhonebook.ContactPanel.actionPerformed(ContactPanel.java:335)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:177)
at java.awt.Dialog$1.run(Dialog.java:1039)
at java.awt.Dialog$3.run(Dialog.java:1091)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1089)
at java.awt.Component.show(Component.java:1419)
at java.awt.Component.setVisible(Component.java:1372)
at java.awt.Window.setVisible(Window.java:801)
at java.awt.Dialog.setVisible(Dialog.java:979)
at JavaPhonebook.ContactPanel.buildEditDialog(ContactPanel.java:284)
at JavaPhonebook.ContactPanel.actionPerformed(ContactPanel.java:328)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
# 13
You're trying to use a method on a variable that's null at line 128 of your ContactIO class. That's in the editContact method.
That much is clearly presented in that stack trace. You are the only one who can tell where line 128 is in that class.
# 14
> You're trying to use a method on a variable that's
> null at line 128 of your ContactIO class. That's in
> the editContact method.
>
> That much is clearly presented in that stack trace.
> You are the only one who can tell where line 128 is
> in that class.
oh sorry forgot to show what line it was: editStatement.close();