使用Prepared Statement选择查询

 阿钱 发布于 2023-01-08 09:45

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

    我将在jTextField中输入客户ID

    输入客户ID后,在数据库中,它将搜索该客户ID的相关信息(customer_id,customer_name,customer_contact).

    收集该特定客户ID的相关信息后,它将显示在jTextField上.

这些是我的代码,如下所示:

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 个回答
  • 查看查询(添加格式以提高可读性,但对此问题无关紧要):

    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 = ?
    

    2023-01-08 09:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有