简体   繁体   English

使用准备好的语句更新

[英]update using prepared statement

I want to update my Database 我想更新我的数据库

  1. When i enter the customer id in jTextbox after that pressing JButton OK then all respective data is extract from the Database is display in the remaining jTextfield in code 当我在按JButton OK之后在jTextbox中输入客户ID时,将从数据库中提取的所有相应数据显示在代码中其余的jTextfield中
  2. 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 在显示了该特定客户ID的所有相应数据库之后,这些数据库再次进行了更新以进行更新,然后单击Update JButton之后,所有相应数据都将更新相应的客户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"); } } } } 

Probably because your field types, especially customer_id , aren't Strings. 可能是因为您的字段类型(尤其是customer_id )不是字符串。 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)
    {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM