Bonsoir à toute la communauté, voici un moment que je bute sur cette erreur. Au fait je veux faire des enregistrements dans une table contenant une clé étrangère.
voici le code au niveau du DAO :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	private String ajouter = "INSERT INTO vehicule(immatriculation,marque,modele,carburant,kilometrage,couleur,typevehicule_idtypevehicule) values(?,?,?,?,?,?,?)";
 
 
/**
         * Methode permettant de faire des enregistrement de vehicules dans notre base
         * de données.
         */
	public Boolean create(Vehicule t) {
 
		int val = -1;
		// creation de preparestatement
 
		try {
			PreparedStatement preparestatement = con.prepareStatement(ajouter);
			preparestatement.setString(1, t.getImmatriculation());
			preparestatement.setString(2, t.getMarque());
			preparestatement.setString(3, t.getModele());
			preparestatement.setString(4, t.getCarburant());
			preparestatement.setInt(5, t.getKilometrage());
			preparestatement.setString(6, t.getCouleur());
			preparestatement.setInt(7, t.getTypeVeh().getIdtypevehicule());
			val = preparestatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return val > 0;
	}

code au niveau du package service:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
IVehiculeDAO vehiculedao = new VehiculeDao();
 
public boolean ajouter(Vehicule vehicule) {
 
		return vehiculedao.create(vehicule);
 
	}
j'ai créé une class model:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class VehiculeModel extends AbstractTableModel {
 
	private IVehiculeService vehiculeService = new VehiculeService();
	private ITypeVehicule typeVehiculeService;
	List<Vehicule> voitures;
	String[] titre = { "IMMATRICULATION", "MARQUE", "MODELE", "CARBURANT", "KILOMETRAGE", "COULEUR", "TYPE VEHICULE" };
	String[] typeVehiculesNames;
	TypeVehicule[] arrayTypeVeh;
 
	public VehiculeModel() {
		vehiculeService = new VehiculeService();
		typeVehiculeService = new STypeVehicule();
		voitures = vehiculeService.affichage();
 
		int typeVehiculeSize = typeVehiculeService.affichage().size();
 
		typeVehiculesNames = new String[typeVehiculeSize];
		arrayTypeVeh = new TypeVehicule[typeVehiculeSize];
		for (int i = 0; i < typeVehiculesNames.length; i++) {
			typeVehiculesNames[i] = typeVehiculeService.affichage().get(i).getTypeveh();
			arrayTypeVeh[i] = typeVehiculeService.affichage().get(i);
		}
 
	}
 
	public int getTypeVehiculeName(String Typeveh) {
		for (int i = 0; i < typeVehiculesNames.length; i++) {
			if (typeVehiculesNames[i].equals(Typeveh)) {
				return i + 1;
			}
		}
		return 0;
	}
 
	public String[] getTypeVehiculeName() {
		return typeVehiculesNames;
	}
 
	public TypeVehicule[] getArrayTypeVeh() {
		return arrayTypeVeh;
	}
 
	@Override
	public String getColumnName(int column) {
		return titre[column];
	}
 
	@Override
	public int getRowCount() {
		return voitures.size();
	}
 
	@Override
	public int getColumnCount() {
		return titre.length;
	}
 
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
 
		case 0:
			return voitures.get(rowIndex).getImmatriculation();
 
		case 1:
			return voitures.get(rowIndex).getMarque();
 
		case 2:
			return voitures.get(rowIndex).getModele();
		case 3:
			return voitures.get(rowIndex).getCarburant();
		case 4:
			return voitures.get(rowIndex).getKilometrage();
		case 5:
			return voitures.get(rowIndex).getCouleur();
		case 6:
			return voitures.get(rowIndex).getTypeVeh().getTypeveh();
 
		default:
			return null;
		}
	}
 
	public void addVehicule(Vehicule vehicule) {
		if (vehiculeService.ajouter(vehicule)) {
			this.voitures.add(vehiculeService.findLast());
			fireTableRowsInserted(voitures.size()-1 , voitures.size()-1);
		}
	}
 
	........
 
}
enfin le code au niveau de l'interface graphique:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
 
 
	private JPanel contentPane;
	private JTextField textimat;
	private JTextField textMarque;
	private JTextField textModel;
	private JTextField textCouleur;
	// private JTable tableVeh;
	private JTable tab_veh;
	private JTextField textkilometrage;
 
String[] namesTypVeh = model.getTypeVehiculeName();
		//TypeVehicule[] typVehArray = model.getArrayTypeVeh();
 
		JComboBox<String> comboTypVeh = new JComboBox<>();
		comboTypVeh.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		comboTypVeh.setBounds(530, 125, 163, 32);
		// remplissage du combo
		comboTypVeh.addItem("");
		for (int i = 0; i < namesTypVeh.length; i++) {
			comboTypVeh.addItem(namesTypVeh[i]);
		}
		contentPane.add(comboTypVeh);
 
		tab_veh = new JTable(model);
		tab_veh.setBounds(10, 313, 683, 64);
		contentPane.add(tab_veh);
		String[] namesTypeVeh = model.getTypeVehiculeName();
		TypeVehicule[] typeVehArray = model.getArrayTypeVeh();
		for (int i = 0; i < namesTypeVeh.length; i++) {
			comboTypVeh.addItem(namesTypeVeh[i]);
		}
 
JButton btnValider = new JButton("VALIDER");
			btnValider.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (textimat.getText().isEmpty() || textMarque.getText().isEmpty() || textModel.getText().isEmpty() || comboTypVeh.getSelectedIndex() == 0 ) {
						JOptionPane.showMessageDialog(null, "Vous navez pas rempli tous les champs", "Erreur", JOptionPane.ERROR_MESSAGE);
					} else {
						TypeVehicule typVeh = typeVehArray[comboTypVeh.getSelectedIndex()];
						model.addVehicule(new Vehicule(textimat.getText(), textMarque.getText(), textModel.getText(),comboCarburant.getSelectedItem().toString(),Integer.parseInt(textkilometrage.getText()),
								textCouleur.getText(),typVeh));
					}
 
					textimat.setText("");
					textMarque.setText("");
					textCouleur.setText("");
					textModel.setText("");
				}
			});





Quand j'essaie de faire un enregistrement je reçois cette erreur:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`bd_projet`.`vehicule`, CONSTRAINT `fk_vehicule_typevehicule1` FOREIGN KEY (`typevehicule_idtypevehicule`) REFERENCES `typevehicule` (`idtypevehicule`) ON DELETE CASCADE ON UPDATE CASCADE)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2441)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2366)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2350)
at ci.objis.projet3.dao.VehiculeDao.create(VehiculeDao.java:58)
at ci.objis.projet3.dao.VehiculeDao.create(VehiculeDao.java:1)
at ci.objis.projet3.service.VehiculeService.ajouter(VehiculeService.java:15)
at ci.objis.projet3.service.VehiculeService.ajouter(VehiculeService.java:1)
at ci.objis.projet3.model.VehiculeModel.addVehicule(VehiculeModel.java:100)
at ci.objis.projet3.iu.AddVehicule$2.actionPerformed(AddVehicule.java:172)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)