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 3 of 3

Thread: MySQL reading, next en previous button

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MySQL reading, next en previous button

    Hi there,

    I am starting programmer and I have a problem with my next en previous button.
    If it is clicked on the end of the records I got the following errors:
    NEXT BUTTON errorjava.lang.ArrayIndexOutOfBoundsException: 12 >= 12
    PREVIOUS BUTTON errorjava.lang.ArrayIndexOutOfBoundsException: -8

    I do not get JOptionPane.showMessageDialog(this,"Dit is het eerste record"); screen

    Please help
    package displayeditor;
     
    	import java.awt.*;
    	import java.sql.*;
    	import java.awt.event.*;
    	import javax.swing.*;
    	import java.util.*;
    	public class Main extends JFrame implements ActionListener
    	{
                     /* Gegevens voor verbinding */
     protected final String dbUser = "***_editor";
     protected final String dbPassword = "*****";
     protected final String dbName = "deb20951_deeditor"; 
     
     /* Gegevens voor verbinding */
     protected String jdbcURL_erde = "jdbc:mysql://www.***.nl:3306/";
     protected String jdbcDriver = "org.gjt.mm.mysql.Driver";
    	Container c;
    	JTextField t1,t2,t3,t4,tf;
    	JLabel l1,l2,l3,l4,lid,lb,logo,logobvl;
    	JButton b1,b2,b3,b4,b5,b6,b7,b8;
    	Connection con;
    	Statement st;
    	PreparedStatement ps;
    	ResultSet rs;
    	Vector v;
    	int index = 0;
    	public Main ()
    //Tekstlabels aanmaken	
            {
    	c=getContentPane();
    	l1=new JLabel("");
    	l2=new JLabel("Display naam");
    	l3=new JLabel("Telefoonnummer");
    	l4=new JLabel("Notitie");
     
            lid=new JLabel("");
    	logo=new JLabel("Created");
            logobvl=new JLabel("BV.");
     
    	v=new Vector(16,1);	
     
    //Tekstvelden aanmaken	
    	t1=new JTextField(10);
    	t2=new JTextField(10);
    	t3=new JTextField(10);
    	t4=new JTextField(10);
     
    //Knoppen definieren en koppelen aan een methode
    	b1=new JButton("Nieuw"); 		b1.addActionListener(this);
    	b2=new JButton("Opslaan");		b2.addActionListener(this);
    	b3=new JButton("Verwijder");		b3.addActionListener(this);
    	b4=new JButton("Update");		b4.addActionListener(this);
    	b5=new JButton("Annuleren");		b5.addActionListener(this);	
    	b6=new JButton("Volgende");		b6.addActionListener(this);		
    	b7=new JButton("Vorige");		b7.addActionListener(this);
    	b8=new JButton("Sluit Programma");	b8.addActionListener(this);
     
     
     //Plaats van de knoppen en velden. left,top,weidth,height
     
            l2.setBounds(10,40,150,20);             t2.setBounds(120,40,150,20);            
            l3.setBounds(10,70,150,20);             t3.setBounds(120,70,150,20);				
    	l4.setBounds(10,100,150,20);            t4.setBounds(120,100,150,20);		   	    
     
            b7.setBounds(10,150,100,40);            b6.setBounds(120,150,100,40);           b4.setBounds(230,150,100,40);
            b1.setBounds(10,200,100,20); 		b2.setBounds(120,200,100,20);           b3.setBounds(230,200,100,20);	 	 
    	b5.setBounds(10,230,100,20); 		b8.setBounds(120,230,210,20);
     
     
    //Alles op z'n plaats        
    	c.add(l2);c.add(l2);c.add(l3);c.add(l4);c.add(lid);	
     
    	c.add(t1);c.add(t2);c.add(t3);c.add(t4);
     
    	c.add(b1);c.add(b2);c.add(b3);c.add(b4);c.add(b5);c.add(b6);c.add(b7);c.add(b8);
     
    //logos op de plaats       
    	logo.setForeground(Color.LIGHT_GRAY);	                                       
    	Font f1=new Font("Arial",Font.BOLD,13);
    	logo.setFont(f1);
    	c.add("South",logo);
            logobvl.setForeground(Color.DARK_GRAY);	                                       
    	Font f2=new Font("Arial",Font.BOLD,24);
    	logobvl.setFont(f2);
            c.add("North",logobvl);
    	l1.setBounds(1,1,1,1);
    	c.add(l1);
     
    //ODBC connectie naar MySQL       
            try            
            {
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	   System.out.print(
    		  "connecting to the database 4" + jdbcURL_erde + dbName + " ... " );
    	   con = DriverManager.getConnection(
    	   jdbcURL_erde + dbName, dbUser, dbPassword );
    	   System.out.println( "done" );
    			st=con.createStatement();
    	rs=st.executeQuery("select * from displayeditor order by de_ID desc");
    	while(rs.next())
    	{
    	v.addElement(rs.getString(1));
    	v.addElement(rs.getString(2));
            v.addElement(rs.getString(3));
    	v.addElement(rs.getString(4));     
     
    	}
     
     
    	}
    	catch(Exception ex)
    	{
    	System.out.println("error is:"+ex);
    	}
     
     
    //aanmaken van het scherm
    		setTitle("Dave");
    		setSize(400,330);
    		setVisible(true);
    		}
    		public static void main(String ages[]) 
    //aanroepen van de klasse
    		 {
            Main Main = new Main();
    		 }
     
    //De methodes achter de knoppen
    	    public void actionPerformed(ActionEvent ae)
    	  {
    		if(ae.getSource()==b8)	// Sluiten Knop
    		{
    			int x;
    			x=JOptionPane.showConfirmDialog(this,"Weet u zeker dat u wilt afsluiten?","Afsluiten",JOptionPane.YES_NO_OPTION);
    			if(x==0)
    			System.exit(0);
    		}
    		else if(ae.getSource()==b1) //Nieuw
    			{
    			t1.requestFocus();
    			t1.setText("");
    			t2.setText("");
    			t3.setText("");
    			t4.setText("");
    		  }
    		  else if(ae.getSource()==b2) //Opslaan
    		  {
    				String s1,s2,s3,s4;
    				s1=t1.getText();
                                    s2=t2.getText();
    				s3=t3.getText();
    				s4=t4.getText();
    				if(s2.length()==0||s3.length()==0)
    				{
    				JOptionPane.showMessageDialog(null,"Vul Display naam en telefoonnummer","Vul juiste gegevens!",JOptionPane.INFORMATION_MESSAGE);
    				return;	}
    					int response=JOptionPane.showConfirmDialog(null,"Wil je een nieuw record opslaan?","Weet je het zeker?",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE	);
    				if(response==0)
    				{		
    				try
                                        //opslaan van een nieuw record in MySQL
    				{
    				ps=con.prepareStatement("insert into displayeditor values(?,?,?,?)");
     
                                    ps.setString(1, null);
    				ps.setString(2,s2);
    				ps.setString(3,s3);
    				ps.setString(4,s4);
     
    				ps.executeUpdate();
    				JOptionPane.showMessageDialog(this,"Het is opgeslagen!");
    			}
    			catch(Exception e1){System.out.println(e1);}
    		}  }
     
    			 else if(ae.getSource()==b3) //Verwijderen
    		 {
    		   try
    		{
    		ps=con.prepareStatement("delete from displayeditor where de_ID=? ");
    		ps.setString(1,t1.getText());
    		ps.executeUpdate();
    		JOptionPane.showMessageDialog(this,"Het is verwijderd!");
     
    			}
    			catch(Exception e2)
    			{
    			System.out.println("error is"+e2);
    			}
     
    		  } 
    		else if(ae.getSource()==b4)//update
    		{
    		try
    		{
    		ps=con.prepareStatement("UPDATE displayeditor SET de_DisplayNaam=?,de_TelefoonNummer=?,de_Notitie=? WHERE de_ID=?" );
                                    ps.setString(1,t2.getText());
    				ps.setString(2,t3.getText());
    				ps.setString(3,t4.getText());
                                    ps.setString(4,t1.getText());
    				ps.executeUpdate();
    				JOptionPane.showMessageDialog(this,"De update is uitgevoerd!");
    				}
    				catch(Exception e4)
    				{
    				System.out.println("error is "+e4);
    				 }
    				 }
     
     
    				else if(ae.getSource()==b5)  // Annuleren
    				{
    				t1.setEnabled(true);
    				int x;
    				x=JOptionPane.showConfirmDialog(this,"Wil je annuleren?","Annuleren",JOptionPane.YES_NO_OPTION);
    				if(x==0)
    				t1.setText("");
    				t2.setText("");
    				t3.setText("");
    				t4.setText("");
     
     
    				}
     
     
    			else if(ae.getSource()==b6) //Volgende
    			{
    			try
    			{
    			if(index==v.capacity()-1)
                            { 
                                JOptionPane.showMessageDialog(this,"Sorry, laatste record");
                            }
    			else
    			{
    			t1.setText(""+v.elementAt(index));
                            index++;	
    			t2.setText(""+v.elementAt(index));
    			index++;
    			t3.setText(""+v.elementAt(index));
    			index++;
    			t4.setText(""+v.elementAt(index));
    			index++;
     
    			} 
    			}
    			catch(Exception e8)
    			{
    				System.out.print("error"+e8);
    			}
    			}
     
     
    			else if(ae.getSource()==b7) // Vorige
    			{
    			try
    			{
    			if(index==1)
                            {
    			JOptionPane.showMessageDialog(this,"Dit is het eerste record");
                            }			
                            else
    			{
    			index=index-8;
    			t1.setText(""+v.elementAt(index));
                            index++;	
                            t2.setText(""+v.elementAt(index));
    			index++;
    			t3.setText(""+v.elementAt(index));
    			index++;
    			t4.setText(""+v.elementAt(index));
    			index++;
     
    		}
    		}
    		catch(Exception e9)
    		{System.out.println("error"+e9);}
    			}
     
     
     
    		}  }
    Last edited by copeg; June 29th, 2011 at 09:26 AM. Reason: Code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MySQL reading, next en previous button

    java.lang.ArrayIndexOutOfBoundsException: -8
    Look at the line of code with the error and see how the array index got to be out of bounds.

    You need to post the FULL text of the error message because it gives the line number where the error occurred.

    What happens if the value of index is 0?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: MySQL reading, next en previous button

    In the future, please wrap your code with the code tags. I've edited your post for you

Similar Threads

  1. Need to uninstall the previous version of java?
    By akanksha4000 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2011, 09:40 AM
  2. Java.jar and Mysql.jar
    By kurt-hardy in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2011, 09:48 AM
  3. slow motion replays of previous games
    By WhatIsJavaAnyhow in forum Java Theory & Questions
    Replies: 10
    Last Post: December 3rd, 2010, 03:45 PM
  4. MySQL Database
    By pjeremy90 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 12th, 2010, 10:34 AM
  5. new arrayList element overwrites all previous
    By twinkletoes in forum Object Oriented Programming
    Replies: 2
    Last Post: April 2nd, 2010, 07:45 AM