This is the code I have so far and this has to be added. I also have my own data
ID: 3847519 • Letter: T
Question
This is the code I have so far and this has to be added. I also have my own database code written
1) Write the code for the 'Update' button. When the user clicks on a ROW in the JTable where the data is displayed, the data from that row should be displayed in the Text boxes on the left. The user will then make changes in the Textboxes and click the 'Update' button to update the DVD Table. Clicking the 'Load Table' button should display the updated table data.
2) Write the code for the 'Delete' button. When the user clicks on a ROW in the JTable where the data is displayed, the data from that row should be displayed in the Text boxes on the left. The user will then click the 'Delete' button to delete the clicked row from the DVD Table. Clicking the 'Load Table' button should display the table data with the row deleted.
HINT:
int row =table.getSlectedRow();
String title = table.getModel().getValueAt(row,1)).toString();
Explanation / Answer
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String val=txt4.getText();
Connection connection = null;
String connn = "jdbc:mysql://localhost:3306/";
String database = "AkshayBisht"; // Database name
String drivers = "com.mysql.jdbc.drivers"; // Driver address
String id = "aaki"; // Connecting to Database with ID and Password
String pswd = "aaki";
try{
Class.forName(drivers);
connection = DriverManager.getConnection(connn+database, id, pswd);
PreparedStatement ps=connection.prepareStatement("DELETE FROM employee WHERE id = ?"); // Defining where to delete the values
ps.setString(1,val);
ps.executeUpdate();
JOptionPane.showMessageDialog(jp3,"Infromation deleted successfully.........");
connection.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(jp3,"Problem in Deleting Record...........");
}
}
});
edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value=txt6.getText();
Connection connection = null;
String connn = "jdbc:mysql://localhost:3306/";
String database = "AkshayBisht";
String drivers = "com.mysql.jdbc.drivers";
String id = "aaki";
String pswd = "aaki";
try{
Class.forName(drivers);
connection = DriverManager.getConnection(connn+database, id, pswd);
PreparedStatement ps=connection.prepareStatement("select * from employee where id=?"); // Defining where to edit the values
ps.setString(1,value);
ResultSet res=ps.executeQuery();
res.next();
txt6.setText(Integer.toString(res.getInt(1)));
txt7.setText(res.getString(2));
txt8.setText(res.getString(3));
txt9.setText(Integer.toString(res.getInt(4)));
connection.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(jp2,"Unable to Edit Data");
}
}
});
edit1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
Connection connection = null;
String connn = "jdbc:mysql://localhost:3306/";
String database = "AkshayBisht"; // Connecting to a particular database
String drivers = "com.mysql.jdbc.drivers";
String id = "aaki"; // Connecting to Database with ID and Password
String pswd = "aaki";
try
{
int x=JOptionPane.showConfirmDialog(jp2,"Edit Confirmed. All Information editted successfully.......");
if(x==0){
try{
String val=txt6.getText();
String val1=txt7.getText();
String val2=txt8.getText();
String val3=txt9.getText();
Class.forName(drivers);
connection = DriverManager.getConnection(connn+database, id, pswd);;
Statement ps=connection.createStatement();
ps.executeUpdate("update employee set emp_name='"+val1+"', emp_address='"+val2+"',
salary='"+val3+"' where emp_id='"+val+"'");
JOptionPane.showMessageDialog(jp2,"Updated successfully........................");
connection.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(jp2,"Problem in Updating. Fix it to carry on");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(jp2,"Error in the Program.....");
}
}
});
Please rate the answer if it helped... Thankyou
Hope it helpsss.......
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.