Reputation: 63
I want to update my Database
After displaying all respective database of that particular customer id that again edited for updation purpose then after clicking on the Update JButton all respective data is updated respective customer id
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public abstract class customer_details extends JFrame implements ActionListener
{
JTextField textFieldId;
JTextField textFieldId1;
JTextField textFieldId2;
JTextField textFieldId3;
JLabel l1;
JLabel l2;
JLabel l3;
JLabel l4;
JLabel l5;
JButton b1,b2;
Container c = getContentPane();
customer_details()
{
super("Shree Datta Digambar");
setBounds(140,250,777,555);
c.setLayout(null);
textFieldId = new JTextField();
textFieldId1 = new JTextField();
textFieldId2 = new JTextField();
textFieldId3 = new JTextField();
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
l1 = new JLabel("Update Customer Details:-");
l2 = new JLabel("Customer Id");
l3 = new JLabel("Customer Id");
l4 = new JLabel("Name");
l5 = new JLabel("Contact");
l1.setBounds(10,10,340,20);
l2.setBounds(10,20,140,70);
l3.setBounds(10,100,140,70);
l4.setBounds(100,100,140,70);
l5.setBounds(270,100,140,70);
textFieldId.setBounds(10,70,70,20);
textFieldId1.setBounds(10,160,70,20);
textFieldId2.setBounds(100,160,150,20);
textFieldId3.setBounds(270,160,90,20);
b1 = new JButton("Ok");
b1.setBounds(100,70,50,20);
b2 = new JButton("Update");
b2.setBounds(380,160,90,20);
c.add(b1);
c.add(b2);
c.add(l1);
c.add(l2);
c.add(l3);
c.add(l4);
c.add(l5);
c.add(textFieldId);
c.add(textFieldId1);
c.add(textFieldId2);
c.add(textFieldId3);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
}
public static void main(String[] args)
{
customer_details eeap=new customer_details() {};
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the button");
if(e.getSource()==b1)
{
try
{
Connection con;
con = DriverManager.getConnection("jdbc:odbc:Dalvi");
java.sql.Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement("SELECT
customer_id,customer_name,customer_contact FROM customer_details WHERE
customer_id = ?");
ps.setString(1,textFieldId.getText());
ResultSet rs1=ps.executeQuery();
while(rs1.next())
{
textFieldId1.setText(rs1.getString(1));
textFieldId2.setText(rs1.getString(2));
textFieldId3.setText(rs1.getString(3));
}
}
catch (SQLException s)
{
System.out.println("SQL code does not execute.");
JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");
}
}
if(e.getSource()==b2)
{
try
{
Connection con;
con = DriverManager.getConnection("jdbc:odbc:Dalvi");
java.sql.Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement("UPDATE customer_details
SET customer_id = ? ,customer_name = ?, customer_contact =?
WHERE customer_id= ?");
ps.setString(1,textFieldId1.getText());
ps.setString(2,textFieldId2.getText());
ps.setString(3,textFieldId3.getText());
ps.setString(4,textFieldId.getText());
ps.executeUpdate();
}
catch (SQLException s)
{
System.out.println("SQL code does not execute.");
JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");
} } } }
Upvotes: 1
Views: 1842
Reputation: 7576
Probably because your field types, especially customer_id
, aren't Strings. Try something like this:
try (
Connection con = DriverManager.getConnection("jdbc:odbc:Dalvi");
PreparedStatement ps = con.prepareStatement("UPDATE customer_details SET customer_id = ?, customer_name = ? , customer_contact =? WHERE customer_id = ?");
)
{
ps.setInt(1, Integer.parseInt(textFieldId.getText()));
ps.setString(2,textFieldId1.getText());
ps.setString(3,textFieldId2.getText());
ps.setInt(4, Integer.parseInt(textFieldId3.getText()));
ps.executeUpdate();
}
catch (SQLException s)
{
System.out.println("SQL code does not execute.");
JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");
}
catch (NumberFormatException e)
{
}
Upvotes: 2