Credits

Monday, July 11, 2011

Java Codes to Add a Record

 In my  previous post, the database system requires adding of record. I decided to include the codes per module, to finally complete the system.

The codes below an action event on Add that clears all the entry fields  for new inputs until a SAVE button is clicked. The rest of the buttons were deactivated too to allow exclusivity.


It is assumed that a connection with the database is made. In my earlier post, I added the set connection commands to do the task.

Keep abreast for the complete codes.


[+/-] show/hide


private void btnMyCompositionAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

try{

if(btnMyCompositionAdd.getText().equalsIgnoreCase("Add")) {
this.txtMyComID.setText("");
this.txtMyCompositionName.setText("");
this.txtMyComID.setEnabled(true);
this.txtMyCompositionName.setEnabled(true);
this.btnMyCompositionDelete.setEnabled(false);
this.btnMyCompositionUpdate.setEnabled(false);
btnMyCompositionAdd.setText("Save");


} else {

SetConnection conn = new SetConnection();
Connection connect = SetConnection.conn;
Statement statement = connect.createStatement();

String qry = "INSERT INTO composition (compositionid,compositionname) " +
"values ('"+txtMyComID.getText().toString()+
"','"+txtMyCompositionName.getText().toString()+"' )";

statement.executeUpdate(qry);
composition();
btnMyCompositionAdd.setText("Add");

this.txtMyComID.setEnabled(true);
this.txtMyCompositionName.setEnabled(true);

this.btnMyCompositionDelete.setEnabled(true);
this.btnMyCompositionUpdate.setEnabled(true);


}
}catch(Exception sqe){
JOptionPane.showMessageDialog(getContentPane(),"Error: "+sqe.getMessage(),
"SQLError",JOptionPane.ERROR_MESSAGE);
}



}

AddThis