Issue
Edit: SOLVED, email address was too long while entering new record - expanding the number of characters on DB solved the issue.
Basic question, but i'm struggling with adding simple sql record via java form into local database. Here is sample of my DB:
This is form created in NetBeans:
And this is my code when i click "Save" button after filling up the data
private void jbtnzapiszpracActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bazadanych","root","");
String queryP = ("INSERT INTO pracownicy (id,first_name,last_name,email,gender,ip_address) VALUES ('"+id+"','"+first_name+"','"+last_name+"','"+email+"','"+gender+"','"+ip_address+"');");
//Example:
//INSERT INTO pracownicy (id,first_name,last_name,email,gender,ip_address) VALUES ('6','Marek','Marecki','[email protected]','Male','0.000.00.0');
PreparedStatement pst = con.prepareStatement(queryP);
pst.setString(1, id.getText());
pst.setString(2, first_name.getText());
pst.setString(3, last_name.getText());
pst.setString(4, email.getText());
if (jrdM.isSelected())
{
gender = "Male";
}
if (jrdK.isSelected())
{
gender = "Female";
}
pst.setString(5, gender);
pst.setString(6,ip_address.getText());
pst.executeUpdate();
}
catch (Exception ex) {
}
this.setVisible(false);
}
I'm pretty sure the connection to database is established as in different form i can preview it all, but the sql database is not updateing with new records when i press "Save"
Thanks in advance and have a great day!
Solution
INFO
There was another issue with the length of data. Logging the exceptions was the key to see what was going on. See comments.
There are no placeholders in your query template string.
Instead of
String queryP = ("INSERT INTO pracownicy (id,first_name,last_name,email,gender,ip_address) VALUES ('"+id+"','"+first_name+"','"+last_name+"','"+email+"','"+gender+"','"+ip_address+"');");
Use
String queryP = "INSERT INTO pracownicy (id,first_name,last_name,email,gender,ip_address) VALUES (?,?,?,?,?,?);";
(I'm not sure if you need the single quotes here)
Answered By - lupz