I’m new in Java programming so that’s why I’m here seeking your kind help stackoverflowers.
I have this problem which I could not find the solution on internet, I hope someone could inform me where is my mistake and how can I fix it.
My problem is that when I try to insert the data, everything goes fine, the window opens, I can insert the data, button looks just fine.
But it gives me this message
java.sql.SQLIntegrityConstraintViolationException: Column ‘prenom’ cannot be null
Which I assume was a problem with php mySQL, then I changed all variables to NULL, activating the button “Null” for all of them.
It then WORKED, but all informations were saved as “null” in my database, which didn’t really solve my problem.
My goal is to insert data (name, lastname, phone, email) into a data base (php mySQL) through eclipse. I did a program which I’m obliged to use window builder to insert that data, so graphical user interface MUST be used
What I expect to happen is to save data normally. Like, name = Mike, lastname = Tyson, phone = +55555, email = mtyson@gmail.com
And it shows in my database, not null message.
Thank you everyone
package interfaceGraphique;
import accesBaseDeDonnees.ClientsBDD;
import entites.Clients;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
public class InterfaceClients extends JFrame {
static InterfaceClients window;
private JPanel contentPane;
private JTextField txtPrenom;
private JTextField txtNom;
private JTextField txtTelephone;
private JTextField txtEmail;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InterfaceClients frame = new InterfaceClients();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InterfaceClients() {
setBackground(new Color(0, 0, 0));
setOpacity(1.0f);
setTitle("Ajouter un client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("Enregistrer");
btnNewButton.addActionListener(new ActionListener() {
private AbstractButton labelResult;
@Override
public void actionPerformed(ActionEvent e) {
String prenom = txtPrenom.getText();
String nom = txtNom.getText();
String telephone = txtTelephone.getText();
String email = txtEmail.getText();
Clients Clients = new Clients(prenom, nom, telephone, email);
ClientsBDD clientsBDD = new ClientsBDD();
Connection cn = clientsBDD.makeConnection();
clientsBDD.insert(Clients, cn);
clientsBDD.closeConnection(cn);
//labelResult = null;
//labelResult.setText("Client ajoute avec succes");
}
});
btnNewButton.setBounds(45, 227, 112, 23);
contentPane.add(btnNewButton);
txtPrenom = new JTextField();
txtPrenom.setBounds(124, 39, 258, 20);
contentPane.add(txtPrenom);
txtPrenom.setColumns(10);
txtNom = new JTextField();
txtNom.setColumns(10);
txtNom.setBounds(124, 82, 258, 20);
contentPane.add(txtNom);
txtTelephone = new JTextField();
txtTelephone.setColumns(10);
txtTelephone.setBounds(124, 125, 258, 20);
contentPane.add(txtTelephone);
txtEmail = new JTextField();
txtEmail.setColumns(10);
txtEmail.setBounds(124, 166, 258, 20);
contentPane.add(txtEmail);
JLabel lblNewLabel = new JLabel("Prenom");
lblNewLabel.setBounds(45, 42, 67, 14);
contentPane.add(lblNewLabel);
JLabel lblMarque = new JLabel("Nom");
lblMarque.setBounds(45, 85, 46, 14);
contentPane.add(lblMarque);
JLabel lblNewLabel_1_1 = new JLabel("Telephone");
lblNewLabel_1_1.setBounds(45, 128, 67, 14);
contentPane.add(lblNewLabel_1_1);
JLabel lblNewLabel_1_1_1 = new JLabel("Email");
lblNewLabel_1_1_1.setBounds(45, 169, 89, 14);
contentPane.add(lblNewLabel_1_1_1);
JLabel labelResult = new JLabel("");
labelResult.setBounds(194, 229, 227, 16);
contentPane.add(labelResult);
}
}
Joao Castilhos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1