i have a mysql database named tutorial which contain a table "devices", i have a swing programme, which insert a row to the table through text box. it works fine for inserting a row, but when i leave the textbox blank and click "ok" button, an empty column is added into the row.how to avoid adding empty row.and avoid adding the row even if a single textbox is empty. i have already defined not null constraint in mysql.plz help.
here is my code:
jb.addActionListener(new ActionListener(){
public void actionPerformed( ActionEvent e ) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loading success!");
String url = "jdbc:mysql://localhost:3306/tutorial";
String name = "root";
String password = "ranjini123";
try {
java.sql.Connection con = DriverManager.getConnection(url, name, password);
System.out.println("Connected.");
String text1=jt1.getText();
String text2=jt2.getText();
String text3=jt3.getText();
String text4=jt4.getText();
String text5=jt5.getText();
ps = con.prepareStatement (
"INSERT INTO devices (asset_id,name,project,emp_id,emp_name) VALUES(?,?,?,?,?)");
try{
ps.setString (1, text1);
ps.setString (2, text2);
ps.setString (3, text3);
ps.setString (4, text4);
ps.setString(5,text5);
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"new device added");
}
catch(NullPointerException n)
{
n.printStackTrace();
}
}
catch (SQLException n)
{
n.printStackTrace();
}
}
catch(Exception n)
{
n.printStackTrace();
}
}
});