Results 1 to 2 of 2
  1. #1
    BobbyH is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Couple of questions about a program

    Java Code:
    package menuKaartBeheer;
    
    import java.sql.*;
    import javax.swing.JOptionPane;
    
    public class Connectie 
    {
    	private String host = "localhost";
    	private String user = "root";
    	private String password = "";
    	private String databasenaam = "kreta";
    	
    	Connection connection;
    	public void displaySQLErrors(Exception e) 
    	{
    		String error;
    		error = "SQL foutmelding: " + e.getMessage();
    		JOptionPane.showMessageDialog(null, error, "foutmelding", JOptionPane.ERROR_MESSAGE);
    		System.out.println("SQL error:" + e.getMessage());
    	}
    
    	public Connectie() 
    	{
    		try 
    		{ 
    			Class.forName("com.mysql.jdbc.Driver"). newInstance();
    		}
    		catch(Exception e)
    		{
    			String error = "Niet mogelijk om de driver te laden";
    			JOptionPane.showMessageDialog(null, error, "foutmelding", JOptionPane.ERROR_MESSAGE);
    
    			System.exit(1);
    		}
    		try 
    		{
    			connection = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.databasenaam + "?user=" + this.user + "&password=" + this.password);
    			String succes = "Er is een connectie met de database";
    			JOptionPane.showMessageDialog(null, succes, "succes", JOptionPane.INFORMATION_MESSAGE);
    		}
    		catch (Exception e) 
    		{
    			displaySQLErrors(e);
    		}
    	 }
    }
    Java Code:
    package menuKaartBeheer;
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Vector;
    
    public class menuKaartBeheer extends DatabaseBewerken
    {
    	JTextField tfdMenuID = new JTextField(4);
    	JTextField tfdNaamGerecht = new JTextField(50);
    	JTextField tfdPrijs = new JTextField(6);
    	JTextArea taIngredienten = new JTextArea(10, 20);
    	JTextArea taRecept = new JTextArea(10, 20);
    	JList lstReceptLijst;
    	
    	JButton btnNieuw;
    	
    	public static void main (String[] args)
    	{
    		menuKaartBeheer gui = new menuKaartBeheer();
    		gui.go();
    	}
    	
    	public void go()
    	{
    		Vector tabel;
    		tabel = leesAlleKaarten();
    		
    		JFrame frame = new JFrame();
    		
    		JPanel panel = new JPanel();
    		JPanel panel1 = new JPanel();
    		
    		JButton btnNieuw = new JButton("Nieuw");
    		JButton btnVoegToe = new JButton("Voeg toe");
    		JButton btnBewerk = new JButton("Bewerk");
    		JButton btnWis = new JButton("Wis");
    		JButton btnStop = new JButton("Stop");
    		
    		btnNieuw.addActionListener(new nieuwListener());
    		btnVoegToe.addActionListener(new voegToeListener());
    		btnBewerk.addActionListener(new bewerkListener());
    		btnWis.addActionListener(new wisListener());
    		btnStop.addActionListener(new stopListener());
    		
    		lstReceptLijst = new JList();
    		lstReceptLijst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		lstReceptLijst.setSelectedIndex(0);
    		lstReceptLijst.addListSelectionListener(new LijstListener());
    			
    		taIngredienten.setLineWrap(true);
    		taRecept.setLineWrap(true);
    		
    		JScrollPane scrIngredienten = new JScrollPane(taIngredienten);
    		scrIngredienten.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scrIngredienten.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		
    		JScrollPane scrRecept = new JScrollPane(taRecept);
    		scrRecept.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scrRecept.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		
    
    		lstReceptLijst = new JList(tabel);
    		
    		JScrollPane scrReceptLijst = new JScrollPane(lstReceptLijst);
    		scrReceptLijst.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scrReceptLijst.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    
    		
    		panel.add(tfdMenuID);
    		panel.add(tfdNaamGerecht);
    		panel.add(tfdPrijs);
    		panel.add(scrIngredienten);
    		panel.add(scrRecept);
    		panel.add(scrReceptLijst);
    		
    		panel1.add(btnNieuw);
    		panel1.add(btnVoegToe);
    		panel1.add(btnBewerk);
    		panel1.add(btnWis);
    		panel1.add(btnStop);
    		
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.getContentPane().add(BorderLayout.NORTH, panel1);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(600,600);
    		frame.setVisible(true);
    	}
    	
    	public class nieuwListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			tfdMenuID.setText("");
    			tfdNaamGerecht.setText("");
    			tfdPrijs.setText("");
    			taIngredienten.setText("");
    			taRecept.setText("");
    		}
    	}
    	
    	public class voegToeListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			try
    			{
    				menu_id = Integer.parseInt(tfdMenuID.getText());
    				setmenu_id(menu_id);
    
    				naam_gerecht = tfdNaamGerecht.getText();
    				setNaam_gerecht(naam_gerecht);
    				prijs = Integer.parseInt(tfdPrijs.getText());
    				setPrijs(prijs);
    				ingredienten = taIngredienten.getText();
    				setIngredienten(ingredienten);
    				recept = taRecept.getText();
    				setRecept(recept);
    				System.out.println(menu_id + naam_gerecht + prijs + ingredienten + recept);
    				schrijfKaart();
    				lstReceptLijst.repaint();
    				lstReceptLijst.setListData(leesAlleKaarten());
    			}
    			catch(Exception e1) 
    			{
    				JOptionPane.showMessageDialog(null, e1.getMessage(), "Fout", JOptionPane.ERROR_MESSAGE);
    			}
    		}
    	}
    	
    	public class bewerkListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    
    		}
    	}
    	
    	public class wisListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			wisKaart();
    		}
    	}
    	
    	public class stopListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	
    	class LijstListener implements ListSelectionListener 
    	{
    		public void valueChanged(ListSelectionEvent e) 
    		{
    			if (e.getValueIsAdjusting()) 
    			{
    				try 
    				{ 
    					int menu_id = Integer.parseInt(((Vector)lstReceptLijst.getSelectedValue()).elementAt(0).toString());
    
    					leesMenus(menu_id);
    
    					tfdMenuID.setText(getmenu_id() + "");
    					tfdNaamGerecht.setText(getNaam_gerecht());
    					tfdPrijs.setText(getPrijs() + "");
    					taIngredienten.setText(getIngredienten());
    					taRecept.setText(getRecept()); 
    				}
    				catch(Exception ex) 
    				{
    					JOptionPane.showMessageDialog(null, ex.getMessage(), "Fout", JOptionPane.ERROR_MESSAGE);
    				}
    			}
    		 }
    	}
    }
    Java Code:
    package menuKaartBeheer;
    
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Vector;
    
    import javax.swing.JOptionPane;
    
    public class DatabaseBewerken
    {
    	Vector vec;
    	Connectie con;
    	int menu_id;
    	String naam_gerecht;
    	int prijs;
    	String ingredienten;
    	String recept;
    
    	public DatabaseBewerken() 
    	{
    		con = new Connectie();
    		vec = new Vector();
    	}
    
    	public DatabaseBewerken(int menu_id, String naam_gerecht, int prijs, String ingredienten, String recept)
    	{
    		this();
    		this.menu_id = menu_id;
    		this.naam_gerecht = naam_gerecht;
    		this.prijs = prijs;
    		this.ingredienten = ingredienten;
    		this.recept = recept;
    	}
    
    	public int getmenu_id() 
    	{
    		return this.menu_id;
    	}
    
    	public void setmenu_id(int menu_id)
    	{
    		this.menu_id = menu_id;
    	}
    	
    	public String getNaam_gerecht() 
    	{
    		return this.naam_gerecht;
    	}
    
    	public void setNaam_gerecht(String naam_gerecht)
    	{
    		this.naam_gerecht = naam_gerecht;
     	}
    	
    	public int getPrijs() 
    	{
    		return this.prijs;
    	}
    
    	public void setPrijs(int prijs)
    	{
    		this.prijs = prijs;
    	}
    	
    	public String getIngredienten() 
    	{
    		return this.ingredienten;
    	}
    
    	public void setIngredienten(String ingredienten)
    	{
    		this.ingredienten = ingredienten;
    	}
    	
    	public String getRecept() 
    	{
    		return this.recept;
    	}
    
    	public void setRecept(String recept)
    	{
    		this.recept = recept;
    	}
    	
    	public void leesMenus(int menu_id) 
    	{
    		try 
    		{ 
    			Statement statement = con.connection.createStatement();
    			String gget;
    			gget = "SELECT * FROM afhaalmenus WHERE menu_id = " + menu_id;
    
    			ResultSet rs = statement.executeQuery(gget);
    			rs.first();
    			this.menu_id = rs.getInt(1);
    			this.naam_gerecht = rs.getString(2);
    			this.prijs = rs.getInt(3);
    			this.ingredienten = rs.getString(4);
    			this.recept = rs.getString(5);
    			System.out.println(this.menu_id);
    		}
    		catch(Exception e)
    		{
    		}
    	}
    
    	public void schrijfKaart() 
    	{
    		try 
    		{ 
    			Statement statement = con.connection.createStatement();
    
    			String sset;
    			sset = "INSERT INTO afhaalmenus (menu_id, naam_gerecht, prijs, ingredienten, recept) VALUES ('" + this.menu_id + "' , '" + this.naam_gerecht + "' , '" + this.prijs + "' , '" + this.ingredienten + "' , '" + this.recept + "')";
    
    			statement.executeUpdate(sset);
    		}
    		catch(Exception e)
    		{
    			System.out.println("foutje");
    		}
    	}
    
    	public void bewerkKaart() 
    	{
    		try 
    		{
    			Statement statement = con.connection.createStatement();
    			String sset;
    			sset = "UPDATE afhaalmenus" + " SET naam_gerecht = '" + this.naam_gerecht + "', " +
    					"prijs = '" + this.prijs + "', " + "ingredienten = '" + this.ingredienten + "', " +
    					"recept = '" + this.recept + "' " +
    					" WHERE afhaalmenus.menu_id = " + this.menu_id;
    
    			statement.executeUpdate(sset);
    		}
    		catch(Exception e)
    	 	{
    	 	}
    	}
    	
    	public void wisKaart()
    	{
    		try 
    		{
    			Statement statement = con.connection.createStatement();
    			String sset;
    			sset = "DELETE FROM afhaalmenus WHERE menu_id = " + this.menu_id;
    			JOptionPane.showMessageDialog(null, sset, "succes", JOptionPane.INFORMATION_MESSAGE);
    
    			statement.executeUpdate(sset);
    
    		}
    		catch(Exception e)
    		{
    		}
    	}
    	
    	public Vector leesAlleKaarten()
    	{
    		try 
    		{ 
    			Statement statement = con.connection.createStatement();
    			String gget;
    			gget = "SELECT DISTINCT * FROM afhaalmenus;";
    
    			ResultSet rs = statement.executeQuery(gget);
    
    			while (rs.next()) 
    			{
    				Vector vRow = new Vector();
    				for(int x = 1; x <= 5; x++)
    				{
    					vRow.addElement(rs.getString(x));
    				}
    				vec.add(vRow);
    			}
    		}
    		catch (Exception e) 
    		{
    			con.displaySQLErrors(e);
    		}
    		return vec;
    	}
    }
    Java Code:
    package menuKaartBeheer;
    
    public class Tester 
    {
    	public Tester() 
    	{
    		 Connectie con = new Connectie();
    	}
    	
    	public static void main(String[] args) 
    	{
    		 Tester test8 = new Tester();
    	}
    }
    First Question:
    How can I delete a record in the database. I'm using a Jlist but the problemen what I'm facing is that de correct Menu_ID is not read. It give me always the ID of "0".

    Second Question:
    How can I edit a Menu if it's selected in the Jlist. So if a menu is selected form the Jlist the correct records are show in the JTextField and JTextArea. When I'm do with editing I have to push de Wijzig butten to save the changes in the database.

    Can somebody help me?
    I have some questions about this program. I can create a record in the mySQL database, but I want to delete one if it is selected in the JList. I can only delete the first record with ID "0". And i want to edit a record if it is selected in the JList.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Couple of questions about a program

    You're going to have to narrow this down.
    Dumping your whole code doesn't help in this case.

    There's too much there to try and spot the relevant bits.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. A couple GUI questions.
    By SpicyHero in forum New To Java
    Replies: 7
    Last Post: 09-22-2013, 04:30 AM
  2. A couple of questions
    By perfidia in forum Java Applets
    Replies: 0
    Last Post: 06-14-2013, 11:05 AM
  3. Couple questions
    By L2Code in forum New To Java
    Replies: 7
    Last Post: 03-22-2013, 09:54 PM
  4. Couple of Generics questions
    By UAF in forum Advanced Java
    Replies: 3
    Last Post: 03-31-2011, 09:37 AM
  5. Couple Questions on Thread
    By Lil_Aziz1 in forum Threads and Synchronization
    Replies: 5
    Last Post: 01-06-2010, 02:02 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
  •