1) Write the code for the \'Update\' button . When the user clicks on a ROW in t
ID: 3847377 • Letter: 1
Question
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.
This is the code I have so far but the code for insert is not working.
Please have it written in Java, I am using eclipse.
JButton btnLoad new JButton (C" Load Table btnLoad addAction Listener new Action ListenerO 1 public void action Performed ActionEvent e) try String query Select from DVD Prepared Statement pStatement conn prepare Statement query ResultSet rset pStatement. execute QueryO table.setModel CDbUtils. resultSetToTableModelCrSet)); Set. Close CD; //conn .close catch CException e2) 1 JOptionPane showMessageDialogGnull, Error loading table");Explanation / Answer
The given code implements the code you asked with a perfect GUI using Swing in JAVA and hope you like it. You will have to change the database to your's database and then it'll be a easy run . Please watch out for the comments and it'll be a easy catch for you to understand the code and be familiar with it.
EmpInfo.java
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
class EmpInfo
{
// Declaring variables for swing
JFrame jf;
JPanel jp1,jp2,jp3;
JTabbedPane tabPane;
ImageIcon image,image1;
JLabel lbl, lbl2, lbl3, lbl4,lbl5,lbl6,lbl7,lbl8,lbl9,lbl10;
JTextField txt,txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9;
JScrollPane scrollp;
JButton save,reset,edit,edit1,del ;
// Creating Constructor to add the GUI to the code.
EmpInfo(){
jf=new JFrame("Form");
jp1=new JPanel(new GridLayout(5,2));
jp2=new JPanel(new GridLayout(5,2));
jp3=new JPanel(new GridLayout(2,2));
tabPane=new JTabbedPane();
lbl=new JLabel("ID of Employee:");
lbl2=new JLabel("Name of Employee:");
lbl3=new JLabel("Address of Employee:");
lbl4=new JLabel("Salary of Employee:");
lbl5=new JLabel("Employee Id to Delete Record:");
lbl7=new JLabel("ID of Employee:");
lbl8=new JLabel("Name of Employee:");
lbl9=new JLabel("Address of Employee:");
lbl10=new JLabel("Salary of Employee:");
txt=new JTextField(12);
txt1=new JTextField(12);
txt2=new JTextField(12);
txt3=new JTextField(12);
txt4=new JTextField(12);
txt5=new JTextField(12);
txt6=new JTextField(12);
txt7=new JTextField(12);
txt8=new JTextField(12);
txt9=new JTextField(12);
save=new JButton(" Insert ");
reset=new JButton(" Reset");
edit=new JButton(" Update ");
edit1=new JButton(" Save ");
del=new JButton(" Delete ");
jp1.add(lbl);
jp1.add(txt);
jp1.add(lbl2);
jp1.add(txt1);
jp1.add(lbl3);
jp1.add(txt2);
jp1.add(lbl4);
jp1.add(txt3);
jp1.add(save);
jp1.add(reset);
jp2.add(lbl7);
jp2.add(txt6);
jp2.add(lbl8);
jp2.add(txt7);
jp2.add(lbl9);
jp2.add(txt8);
jp2.add(lbl10);
jp2.add(txt9);
jp2.add(edit);
jp2.add(edit1);
jp3.add(lbl5);
jp3.add(txt4);
jp3.add(del);
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
txt.setText("");
txt1.setText("");
txt2.setText("");
txt3.setText("");
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String val=txt.getText();
String val1=txt1.getText();
String val2=txt2.getText();
String val3=txt3.getText();
Connection connection = null;
String connn = "jdbc:mysql://localhost:3306/"; // Establishing Connection
String database = "AkshayBisht";
String drivers = "com.mysql.jdbc.drivers";
String id = "aaki";
String pswd = "aaki";
System.out.println(val+val1+val2+val3);
try{
Class.forName(drivers);
connection = DriverManager.getConnection(connn+database, id, pswd); // Deriving connection to the database
PreparedStatement ps=connection.prepareStatement("insert into employee(id,name,ddress,sal) values(?,?,?,?)"); // Defining where to insert the values
ps.setString(1,val);
ps.setString(2,val1);
ps.setString(3,val2);
ps.setString(4,val3);
ps.executeUpdate();
JOptionPane.showMessageDialog(jp1,"Information inserted successfully........");
connection.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(jp1,"Something missing in submitting Information");
}
}
});
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.....");
}
}
});
}
void display()
{
jf.getContentPane().add(tabPane);
tabPane.addTab("Add Information",jp1);
tabPane.addTab("Edit Infromation",jp2);
tabPane.addTab("Delete Infromation",jp3);
jf.setSize(350,180);
jf.setVisible(true);
jf.setResizable(true);
}
public static void main(String args[]){
EmpInfo emppp=new EmpInfo(); // Calling the Main Function and Display()
emppp.display();
}
}
Please rate the answer if it helps..... Thankyou
Hope it helpss......
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.