Java MySQL PreparedStatement SELECT语句 [英] Java MySQL PreparedStatement SELECT Statement

查看:473
本文介绍了Java MySQL PreparedStatement SELECT语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库和一个Java程序.我正在尝试编写代码,以便如果在文本字段中输入了MockID,然后按下了提交按钮,则应从数据库中检索根据输入的Mock ID的详细信息,并将其显示在文本区域中.下面是我编写的代码.我修改后,该代码现在可以正常工作.但是,在文本区域中,它实际上不是显示记录中给定模拟ID的相关信息,而只是显示文本. 有人可以建议吗?

I have a database and a Java Program. I am trying to write a code, so that if in a textfield a MockID is entered, and the submit button is pressed, the details according to that entered Mock ID should be retrieved from the database and displayed in a textarea. below is the code that i have written. The code now works after amendments i have made. However, in the textarea, its not actually displaying the relevant information from that record for the given Mock Id, but just text. Could someone please advise ?

    JButton button = new JButton("Submit");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


        String mockId = textField.getText();    

        try {

            String sql = "SELECT MockID, Subject, Year, Date FROM mockexam WHERE MockID =?";    

            PreparedStatement prest = con.prepareStatement(sql);
            prest.setString(1, mockId);

            prest.executeQuery();
            textArea.append("MockID, Subject, Year, Date");
            JOptionPane.showMessageDialog(frmFindMock, "Record has been updated.");


        }

        catch (SQLException e) {
        //System.out.println("Record couldn't be added!");
        e.printStackTrace();
        JOptionPane.showMessageDialog(frmFindMock, "Record couldn't be updated. Please try again.");
        }
        }

        });

    button.setBounds(303, 60, 75, 23);
    panel_1.add(button);

推荐答案

?是绑定变量占位符.您只有1.那么为什么要尝试绑定4件事?

The ? is the bind variable placeholder. you only have 1. so why are you trying to bind 4 things?

这篇关于Java MySQL PreparedStatement SELECT语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆