Results 1 to 6 of 6
  1. #1
    nanna is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default phonebook update

    hi,
    iam trying to add and save names and numbers to a database, but when i click any button nothing happens. i do not wher is the problem in my program???
    pls if you can help


    Java 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)
    		  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());
           }
       }
    
      
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Maybe I missed it, but do you add ActionListeners (or in this case this) to the buttons?

  3. #3
    nanna is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default phone directory

    ya, i forget to add actionlistener. but after adding actionlistener i`ve got the following exception error when trying to press 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(Unk nown 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.mouseRe leased(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(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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.pumpOneEventForFilter s(Unknown Source
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(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

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  5. #5
    nanna is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default phone directory

    personAdd method should add a name and number to database

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by nanna View Post
    personAdd method should add a name and number to database
    Well, if you haven't changed the class since you posted these error messages:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Ghu.personAdd(Ghu.java:140)
    I'd look awfully close at line 140 of the Ghu class, personAdd method. Somewhere on that line, you're trying to use an object that hasn't yet been instantiated.

Similar Threads

  1. WebSite Update!
    By Xyle in forum Reviews / Advertising
    Replies: 1
    Last Post: 02-16-2009, 09:47 AM
  2. How to update my jdk???
    By low224 in forum New To Java
    Replies: 4
    Last Post: 01-04-2009, 05:51 PM
  3. SQL update problem
    By skiing in forum New To Java
    Replies: 9
    Last Post: 11-21-2008, 07:54 PM
  4. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 12:49 AM
  5. dynamic update in swt
    By sandor in forum SWT / JFace
    Replies: 0
    Last Post: 05-14-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •