Query & Update/Edit user entered Data in Java & My Sql

First retrieve Data(Query) from MqSql Database and make desired changes in it and update it to Database.

When user click on update button following code executes..

JButton btnNewButton_1 = new JButton("Update");

btnNewButton_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

}

});

btnNewButton_1.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

try

{

updaterec();

}

catch(Exception ex){System.out.println(e);}

finally {

System.out.println("Updation Complete");

    }

}

});

////////////////////////////

public void updaterec() throws Exception{

PreparedStatement ps = null;

try {

Connection conn=getConnection();

String query= "update sch_db.student set st_class=?,st_section=?,stream=?,bus_stop = ?,stoppage=?,st_address=?,st_city=?,where substr(st_id,1,4) = ? ";

conn.setAutoCommit(false);

ps = conn.prepareStatement(query);

ps.setString(1,Stud_class.getText());

ps.setString(2,Stud_section.getText());

ps.setString(3,dispstream.getText());

ps.setString(4,Stud_routeno.getText());

ps.setString(5,Stud_stoppage.getText());

ps.setString(6,Stud_addr.getText());

ps.setString(7,stud_city.getText());

ps.setString(8,Adm_No.getText());

ps.executeUpdate();

conn.commit();

JOptionPane.showMessageDialog(null, "Record Updated","Update Confirmation",JOptionPane.DEFAULT_OPTION);

}

catch (Exception e) {

         e.printStackTrace();

   }

}

//////////////////////////////

Make sure that Connection between Database & Application is in place, otherwise update would not

happen.

Next post will be on Deletion of Data...

 

 

Comments