Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java MS Access not connection help please.

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java MS Access not connection help please.

    I need my java program to insert data into my access database. I get no errors in my code, but it never inserts, deletes the data in my database. please help. here is my code:

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class INS extends Frame implements ActionListener{
    	Frame f;
    	Label l1, l2;
    	TextField t1, t2;
    	Button b1, b2, b3, b4, b5;
    	Connection c;
    	Statement s;
    	ResultSet r;
    	INS (){
     
    		try{
    			f=new Frame();
    			f.setLayout(null);
    			f.setVisible(true);
    			f.setSize(800, 600);
    			l1=new Label("ID");
    			l1.setBounds(50, 100, 100, 50);
    			f.add(l1);
    			l2=new Label("Name");
    			l2.setBounds(50, 150, 100, 50);
    			f.add(l2);
    			t1=new TextField();
     
    			t1.setBounds(150, 100, 100, 40);
    			f.add(t1);
    			t2=new TextField();
    			t2.setBounds(150, 150, 100, 40);
    			f.add(t2);
     
    			b1= new Button("Insert");
    			b1.setBounds(200, 300, 75, 50);
    			f.add(b1);
    			b1.addActionListener(this);
     
    			b2= new Button("Update");
    			b2.setBounds(300, 300, 75, 50);
    			f.add(b2);
    			b2.addActionListener(this);
     
    			b3= new Button("Delete");
    			b3.setBounds(400, 300, 75, 50);
    			f.add(b3);
    			b3.addActionListener(this);
     
    			b5= new Button("EXIT");
    			b5.setBounds(600, 300, 75, 50);
    			f.add(b5);
    			b5.addActionListener(this);
     
     
    			Class.forName("sun.jdbc.obdc.JdbcObdcDriver");
    			c= DriverManager.getConnection("jdbc:obdc:ADB");
    			s=c.createStatement();
     
    			}catch(Exception e){}
     
    		}//ends INS Constructor
     
    	public void actionPerformed(ActionEvent ae){
    		try{
    			if(ae.getSource()==b1){
    				String s1="INSERT INTO USER(id, name) VALUES("+t1.getText()+",'"+t2.getText() + "')";
    				System.out.println(s1);
    				s.executeUpdate(s1);
    				r=s.executeQuery("SELECT * FROM USER");
    				t1.setText("");
    				t2.setText("");
     
    				}else if(ae.getSource()==b3){
     
    				String s3="DELETE FROM USER WHERE ID="+t1.getText();
    				System.out.println(s3);
    				s.executeUpdate(s3);
    				r=s.executeQuery("SELECT * FROM USER");
    				t1.setText("");
    				t2.setText("");
     
     
    				}else if(ae.getSource()==b5){
     
    					c.close();
    					f.dispose();
     
    					}
     
    		}catch(Exception e){}
     
    }
     
    	public static void main(String args[]){
    		new INS();
     
    		}
     
    	}
    I am not very good at programming please keep it simple for me to understand.
    Last edited by notgettingthis; March 29th, 2014 at 01:46 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java MS Access not connection help please.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    One problem I see is the empty catch block. You need to add a call to the printStackTrace() method so that there is a print out when an exception is thrown.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java MS Access not connection help please.

    okay thanks I fixed the post.

    I do not really understand coding...could you explain how to use that I would really appreciate it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java MS Access not connection help please.

    Add
    e.printStackTrace();
    inside of the {}s following catch
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java MS Access not connection help please.

    okay now I got a long list of error messages :

    INSERT INTO USER(id, name) VALUES(2,'fd')
    java.lang.NullPointerException
    at INS.actionPerformed(INS.java:68)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(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)

    note: I ran this in eclipse.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java MS Access not connection help please.

    java.lang.NullPointerException
    at INS.actionPerformed(INS.java:68)
    The statement on line 68 was executed with a null value.
    Look at line 68 and find the variable with the null value. Then backtrack in the code to see why that variable has a null value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java MS Access not connection help please.

    open I fixed some of my code

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class INS extends Frame implements ActionListener{
    	Frame f;
    	Label l1, l2;
    	TextField t1, t2;
    	Button b1, b2, b3, b4, b5;
    	Connection c;
    	Statement s;
    	ResultSet r;
    	INS (){
     
    		try{
    			f=new Frame();
    			f.setLayout(null);
    			f.setVisible(true);
    			f.setSize(800, 600);
    			l1=new Label("ID");
    			l1.setBounds(50, 100, 100, 50);
    			f.add(l1);
    			l2=new Label("Name");
    			l2.setBounds(50, 150, 100, 50);
    			f.add(l2);
    			t1=new TextField();
     
    			t1.setBounds(150, 100, 100, 40);
    			f.add(t1);
    			t2=new TextField();
    			t2.setBounds(150, 150, 100, 40);
    			f.add(t2);
     
    			b1= new Button("Insert");
    			b1.setBounds(200, 300, 75, 50);
    			f.add(b1);
    			b1.addActionListener(this);
     
    			b2= new Button("Update");
    			b2.setBounds(300, 300, 75, 50);
    			f.add(b2);
    			b2.addActionListener(this);
     
    			b3= new Button("Delete");
    			b3.setBounds(400, 300, 75, 50);
    			f.add(b3);
    			b3.addActionListener(this);
     
    			b5= new Button("EXIT");
    			b5.setBounds(600, 300, 75, 50);
    			f.add(b5);
    			b5.addActionListener(this);
     
     
    			Class.forName("sun.jdbc.obdc.JdbcObdcDriver");
    			c= DriverManager.getConnection("jdbc:obdc:ADB");
    			s=c.createStatement();
     
    			}catch(Exception e){}
     
    		}//ends INS Constructor
     
    	public void actionPerformed(ActionEvent ae){
     
    		try{
     
    			if(ae.getSource()==b1){
     
    				String s1="INSERT INTO USER(id, name) VALUES("+t1.getText()+",'"+t2.getText() + "')";
    				System.out.println(s1);
    				s.executeUpdate(s1);
    				r=s.executeQuery("SELECT * FROM USER");
    				t1.setText("");
    				t2.setText("");
     
     
    			}else if(ae.getSource()==b2){
     
    				String s2="UPDATE USER SET NAME='"+t2.getText()+"'WHERE ID="+t1.getText();
    				System.out.println(s2);
    				s.executeUpdate(s2);
    				r=s.executeQuery("SELECT * FROM USER");
    				t1.setText("");
    				t2.setText("");
     
     
    				}if(ae.getSource()==b3){
     
    				String s3="DELETE FROM USER WHERE ID="+t1.getText();
    				System.out.println(s3);
    				s.executeUpdate(s3);
    				r=s.executeQuery("SELECT * FROM USER");
    				t1.setText("");
    				t2.setText("");
     
     
    				}else if(ae.getSource()==b5){
     
    					c.close();
    					f.dispose();
     
    					}
     
    		}catch(Exception e){e.printStackTrace();}
     
    }
     
    	public static void main(String args[]){
    		new INS();
     
    		}
     
    	}


    now I get these errors:

    INSERT INTO USER(id, name) VALUES(2,'ad')
    java.lang.NullPointerException
    at INS.actionPerformed(INS.java:71)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(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)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java MS Access not connection help please.

    java.lang.NullPointerException
    at INS.actionPerformed(INS.java:71)
    Now the null value is on line 71

    The code tags for your post need fixing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java MS Access not connection help please.

    Fixed. (Still learning)

    what do I do with a null value?
    I need whatever values I insert to save to the database. The only null I see is the setLayout.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java MS Access not connection help please.

    what do I do with a null value?
    Test if a variable has a null value instead of a good value and change execution flow to react to it so that it does not cause an exception.

    NOTE: There are still catch blocks without the call to printStackTrace().
    All the catch blocks should have that.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how connection java app to access database that in server
    By eng.hazem in forum JDBC & Databases
    Replies: 2
    Last Post: July 29th, 2013, 06:24 AM
  2. Shared connection or individual connection?
    By newbie14 in forum Java Networking
    Replies: 6
    Last Post: January 12th, 2013, 04:10 AM
  3. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  4. MS Access database connection problem
    By faysal40 in forum JDBC & Databases
    Replies: 1
    Last Post: August 29th, 2011, 01:22 AM
  5. Replies: 0
    Last Post: March 26th, 2011, 11:07 AM

Tags for this Thread