hi,
Iam trying to add names and numbers to a database then save them by updating them. i run my program, but when i press any button nothing happens?????
pls help me, i do not know where is the problem in my program????

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Ghu extends JFrame implements ActionListener {

  final static String 
  dbURL="jdbc:odbc:addressesdb";

   private final String WINDOW_TITLE= "AddressBook";
        private final int WINDOW_WIDTH= 400;
        private final int WINDOW_HEIGHT= 300;
	
   
        private JPanel adPanel= new JPanel();
        private JPanel delPanel= new JPanel();
        private JPanel updPanel= new JPanel();
        private JLabel adNameLabel=  new JLabel("Name");
        private JLabel adNumLabel= new JLabel("Number");
        private JLabel delNumLabel= new JLabel("Number");
        private JLabel updNameLabel=  new JLabel("Name");
        private JLabel updNumLabel= new JLabel("Number");
      
        private JTextField adNameField= new JTextField(20);
        private JTextField adNumField= new JTextField(20);
        private JTextField updNameField= new JTextField(20);
        private JTextField updNumField= new JTextField(20);
        private JTextField delNumField= new JTextField(20);
        private JButton adButton= new JButton("Add");
        private JButton delButton= new JButton("Delete");
        private JButton updButton= new JButton("Update");
        private JButton exitButton= new JButton("Exit");
       
   	
  

  Connection conn;
  Statement stmt;
  ResultSet rset;

  public static void main(String args[]) {
	new Ghu();
  }

  public Ghu() {
	setTitle(WINDOW_TITLE);
   	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
   	setVisible(true);
   	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        buildAdPanel();
        buildDelPanel();
        buildUpdPanel();
        add(adPanel,BorderLayout.NORTH);
        add(delPanel,BorderLayout.CENTER);
        add(updPanel,BorderLayout.SOUTH);
  }


public boolean isConnected(){
  boolean connected=false;
  try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");	

	conn = 	DriverManager.getConnection(dbURL); 
        stmt = conn.createStatement();
	connected=true;
  }
  catch (ClassNotFoundException e) {
        System.err.println("Can't find JDBC to ODBC Bridge");    

  }
  catch(SQLException e) {
    System.err.println("An SQL exception occurred while trying 

to connect to the server: " +e.getMessage());
  }
  return connected;
}

public void actionPerformed(ActionEvent evt)  {
	try {
		String command=evt.getActionCommand();
		
		
		if (evt.getSource()==adButton)
		  processAdd();
		else if (evt.getSource()==delButton)
		  processDelete();
		else if (evt.getSource()==updButton)
		  processUpdate();
		else if (evt.getSource()==exitButton) {
    	  stmt.close();
    	  conn.close();
		  System.exit(0);
		}
	}
	catch (SQLException e) {
	       System.err.println("An SQL exception occurred 

while exiting: " +e.getMessage());
	}
   }

  public void buildAdPanel() {
        adPanel.setLayout(new GridLayout(3,2));
        adPanel.add(adNameLabel);
        adPanel.add(adNameField);
        adPanel.add(adNumLabel);
        adPanel.add(adNumField);
        adPanel.add(adButton);
       }


  public void buildDelPanel() {
         delPanel.setLayout(new GridLayout(2,2));
         delPanel.add(delNumLabel);
         delPanel.add(delNumField);
         delPanel.add(delButton);
                       
       }
  public void buildUpdPanel() {
        updPanel.setLayout(new GridLayout(3,2));
        updPanel.add(updNameLabel);
        updPanel.add(updNameField);
        updPanel.add(updNumLabel);
        updPanel.add(updNumField);
        updPanel.add(updButton);
        updPanel.add(exitButton);
	
     }

  
  private void processAdd() {
  	String sql;
  	int nrows;
  	sql="insert into addressesdb values(?,?)";
  	try {
	  PreparedStatement psmt=conn.prepareStatement(sql);
	  psmt.setString(1,adNumField.getText());
	  psmt.setString(2,adNameField.getText());
  	  nrows=psmt.executeUpdate();
  	  psmt.close();
  	  JOptionPane.showMessageDialog(null,"The name is 

saved");
       }
       catch (SQLException e) {
          System.err.println("An SQL exception occurred while 

Inserting: " +e.getMessage());
       }
  }

  private void processUpdate() {
  	String sql;
  	int nrows;
  	sql="update addressesdb set NAMES=? where NUMBERS=?";
  	try {
   	  PreparedStatement psmt=conn.prepareStatement(sql);
   	  psmt.setString(1,updNameField.getText());
   	  psmt.setString(2,updNumField.getText());
 	  nrows=psmt.executeUpdate();
 	  psmt.close();
  	 JOptionPane.showMessageDialog(null,"The database is 

updated");
       }
        catch (SQLException e) {
          System.err.println("An SQL exception occurred while 

Updating: " +e.getMessage());
       }
  }

  private void processDelete() {
  	String sql;
  	int nrows;
  	sql="delete from addressesdb where NUMBERS=?";
  	try {
   	  PreparedStatement psmt=conn.prepareStatement(sql);
   	  psmt.setString(1,delNumField.getText());
 	  nrows=psmt.executeUpdate();
 	  psmt.close();
  	  JOptionPane.showMessageDialog(null,"The Number is 

deleted");
       }
       catch (SQLException e) {
         System.err.println("An SQL exception occurred while 

Deleting: " +e.getMessage());
       }
   }

  
}

In order for the actionPerformed method to be called, you need to use whateverThingItIs.addActionListener(Ghu); where Ghu is an instance of your Ghu class. So, for example, if you had a JButton called button and a Ghu called myGHU, it would have to say button.addActionListener(myGHU); . Your code is pretty hard to read but it seems like you never used this method, so that is why actionPerformed will never be called & it will never work.

In order for the actionPerformed method to be called, you need to use whateverThingItIs.addActionListener(Ghu); where Ghu is an instance of your Ghu class. So, for example, if you had a JButton called button and a Ghu called myGHU, it would have to say button.addActionListener(myGHU); . Your code is pretty hard to read but it seems like you never used this method, so that is why actionPerformed will never be called & it will never work.

hi,
thanks, yes i forget to add actionlistener.
But after adding actionlistener, i get the following exception error when press on any button :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ghu.personAdd(Ghu.java:140)
at Ghu.actionPerformed(Ghu.java:85)
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 S
ce)
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.Window.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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

There is no such method called personAdd in Ghu in what you've posted, yet there is an error saying there is. .

There is no such method called personAdd in Ghu in what you've posted, yet there is an error saying there is..

yes there is such a method in my program since i changed the name of processAdd method into personAdd, but when i press any button i've got the same error??????

Post your updated code, then, and your updated errors (if you have any new/different ones). Post your code in code tags and for the first CODE, put so it will have line numbers.[CODE=Java] so it will have line numbers.

Here is the updated code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Ghu extends JFrame implements ActionListener {

  final static String 
  dbURL="jdbc:odbc:addressesdb";

   private final String WINDOW_TITLE= "AddressBook";
        private final int WINDOW_WIDTH= 400;
        private final int WINDOW_HEIGHT= 300;
	
   
        private JPanel adPanel= new JPanel();
        private JPanel delPanel= new JPanel();
        private JPanel updPanel= new JPanel();
        private JLabel adNameLabel=  new JLabel("Name");
        private JLabel adNumLabel= new JLabel("Number");
        private JLabel delNumLabel= new JLabel("Number");
        private JLabel updNameLabel=  new JLabel("Name");
        private JLabel updNumLabel= new JLabel("Number");
      
        private JTextField adNameField= new JTextField(20);
        private JTextField adNumField= new JTextField(20);
        private JTextField updNameField= new JTextField(20);
        private JTextField updNumField= new JTextField(20);
        private JTextField delNumField= new JTextField(20);
        private JButton adButton= new JButton("Add");
        private JButton delButton= new JButton("Delete");
        private JButton updButton= new JButton("Update");
        private JButton exitButton= new JButton("Exit");
       
   	
  

  Connection conn;
  Statement stmt;
  ResultSet rset;

  public static void main(String args[]) {
	new Ghu();
  }

  public Ghu() {
	setTitle(WINDOW_TITLE);
   	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
   	setVisible(true);
   	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        buildAdPanel();
        buildDelPanel();
        buildUpdPanel();
        add(adPanel,BorderLayout.NORTH);
        add(delPanel,BorderLayout.CENTER);
        add(updPanel,BorderLayout.SOUTH);
  }


public boolean isConnected(){
  boolean connected=false;
  try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");	

	conn = 	DriverManager.getConnection(dbURL); 
        stmt = conn.createStatement();
	connected=true;
        
  }
  catch (ClassNotFoundException e) {
        System.err.println("Can't find JDBC to ODBC Bridge");      }
  catch(SQLException e) {
    System.err.println("An SQL exception occurred while trying to connect to the server: " +e.getMessage());
  }
  return connected;
}

public void actionPerformed(ActionEvent evt)  {
	try {
		String command=evt.getActionCommand();
		
		if (evt.getSource()==adButton) {
		  personAdd();
               }
		else if (evt.getSource()==delButton) 
		  personDelete();
                
		else if (evt.getSource()==updButton) 
		  personUpdate();
                
		else if (evt.getSource()==exitButton) {
    	  stmt.close();
    	  conn.close();
               
         	  System.exit(0);
	      }
       }
	catch (SQLException e) {
	       System.err.println("An SQL exception occurred while exiting: " +e.getMessage());
	}
   }

  private void buildAdPanel() {
        adPanel.setLayout(new GridLayout(3,2));
        adPanel.add(adNameLabel);
        adPanel.add(adNameField);
        adPanel.add(adNumLabel);
        adPanel.add(adNumField);
        adPanel.add(adButton);
        adButton.addActionListener(this);
       }


  private void buildDelPanel() {
         delPanel.setLayout(new BorderLayout());
         delPanel.add(delNumLabel,BorderLayout.WEST);
         delPanel.add(delNumField,BorderLayout.CENTER);
         delPanel.add(delButton, BorderLayout.SOUTH);
         delButton.addActionListener(this);
                       
       }
  private void buildUpdPanel() {
        updPanel.setLayout(new GridLayout(3,3));
        updPanel.add(updNameLabel);
        updPanel.add(updNameField);
        updPanel.add(updNumLabel);
        updPanel.add(updNumField);
        updPanel.add(updButton);
        updButton.addActionListener(this);
        updPanel.add(exitButton);
        exitButton.addActionListener(this);
	
     }

  
  private void personAdd() {
  	String sql;
  	int nrows;
  	sql="insert into addressesdb values(?,?)";
  	try {
	  PreparedStatement psmt=conn.prepareStatement(sql);
	  psmt.setString(1,adNumField.getText());
	  psmt.setString(2,adNameField.getText());
  	  nrows=psmt.executeUpdate();
  	  psmt.close();
  	 JOptionPane.showMessageDialog(null,nrows + "The name is saved");
       }
       catch (SQLException e) {
          System.err.println("An SQL exception occurred while Inserting: " +e.getMessage());
       }
  }

  private void personUpdate() {
  	String sql;
  	int nrows;
  	sql="update addressesdb set NAMES=? where NUMBERS=?";
  	try {
   	  PreparedStatement psmt=conn.prepareStatement(sql);
   	  psmt.setString(1,updNameField.getText());
   	  psmt.setString(2,updNumField.getText());
 	  nrows=psmt.executeUpdate();
 	  psmt.close();
  	 JOptionPane.showMessageDialog(null,nrows + "The database is updated");
       }
        catch (SQLException e) {
          System.err.println("An SQL exception occurred while Updating: " +e.getMessage());
       }
  }

  private void personDelete() {
  	String sql;
  	int nrows;
  	sql="delete from addressesdb where NUMBERS=?";
  	try {
   	  PreparedStatement psmt=conn.prepareStatement(sql);
   	  psmt.setString(1,delNumField.getText());
 	  nrows=psmt.executeUpdate();
 	  psmt.close();
  	  JOptionPane.showMessageDialog(null,nrows + "The Number is deleted");
       }
       catch (SQLException e) {
         System.err.println("An SQL exception occurred while Deleting: " +e.getMessage());
       }
   }


And here is the error :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Ghu.personAdd(Ghu.java:142)
        at Ghu.actionPerformed(Ghu.java:84)
        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 Sou
ce)
        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.Window.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)

  
}

Hi nanna,

Sorry to weigh in on this conversation at such a late stage, but I think I see your problem. The compiler is pointing you to it with the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ghu.personAdd(Ghu.java:142)

It is saying that something is calling a method on line 142 in the Ghu class, but that something is null. A null object (one that hasn't been instantiated properly) cannot call a method. In your case, the variable conn has not been created before the call to conn.prepareStatement(sql); on line 142. So you need to call your isConnected method before this call in order to create conn.

HTH,
darkagn

Hi nanna,

Sorry to weigh in on this conversation at such a late stage, but I think I see your problem. The compiler is pointing you to it with the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ghu.personAdd(Ghu.java:142)

It is saying that something is calling a method on line 142 in the Ghu class, but that something is null. A null object (one that hasn't been instantiated properly) cannot call a method. In your case, the variable conn has not been created before the call to conn.prepareStatement(sql); on line 142. So you need to call your isConnected method before this call in order to create conn.

HTH,
darkagn

Thanks, I think now I have connection when i add
conn = DriverManager.getConnection(dbURL); method before PreparedStatement.
But when I try to add a name and number I get a message says that name is saved, but when I display my database I cannot find the new name?? And it is the same when I try to delete a number from the database table I get a message says that the number is deleted but it still there in the database although I press (update ) button??.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.