I'm doing a sample project using jdbc driver. My problem is if I enter a null in my 2 textfield. The null should not be load into the database? Is there anyway how to avoid inserting a empty field in database? Any help will appreciate.
//Execute a query
System.out.println("Creating Statement..");
String query = "INSERT INTO INFO (FIRSTNAME,LASTNAME) VALUES (?,?)";
myStatement = con.prepareStatement(query);
myStatement.setString(1, firstnameTf.getText());
myStatement.setString(2, lastnameTf.getText());
myStatement.executeUpdate();
if (firstnameTf.getText().isEmpty() || lastnameTf.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Please Insert");
}
myStatement.close();
con.close();
INFO? The simplest way is to declare the names asNOT NULL- you will get an exception if you try to insertnull.NOT NULLcheck in database it will throw an exception and catch it in catch block and print any msg