使用Prepared Statement选择查询

时间:2014-06-23 07:03:34

标签: java sql jdbc

我不知道我的代码有什么问题。以下是我的代码中的条件:

  1. 我将在jTextField中输入客户ID
  2. 输入客户ID后,在数据库中,它将搜索该客户ID的相关信息(customer_id,customer_name,customer_contact)。
  3. 收集该特定客户ID的相关信息后,它将显示在jTextField上。
  4. 以下是我的代码:

    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.*;
    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() {};
    }
     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));   
                }                    
                textFieldId.setText("");
            }
            catch (SQLException s) 
            {
                System.out.println("SQL code does not execute.");
                JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly");
            }
        }  
    }
    

    }

1 个答案:

答案 0 :(得分:6)

查看查询(添加格式以提高可读性,但对此问题无关紧要):

SELECT customer_id,customer_name,customer_contact 
FROM   customer_details 
WHERE  customer_id = ' ?'

通过使用单引号(?)包围',您已将其转换为SQL字符文字。这样,JDBC 将其识别为特殊字符,并且无法绑定它。如果删除它们,JDBC将能够正确绑定(并且也会处理数据类型):

SELECT customer_id,customer_name,customer_contact
FROM   customer_details 
WHERE  customer_id = ?