当输入一个 Jtextfield 时,从数据库中获取数据到 Jtextfield [英] Fetch data from database into Jtextfield when One Jtextfield is Typed

查看:46
本文介绍了当输入一个 Jtextfield 时,从数据库中获取数据到 Jtextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个连接到 oracle 数据库的 Swing 应用程序,我想要它,一旦我在 JTextField 中键入一个值,JFrame 上的其他 JTextfields 就会加载数据库中的后续数据,但我没有似乎达到了这一点.我已经尝试了以下代码,但它什么也没得到.

Hi Guys I have a Swing Application that is connected to an oracle database, I want it such that Once I type a Value into the JTextField, the other JTextfields on the JFrame are loaded with subsequent data from the database but I do not seem to achieve this. I have tried the Following code but it fetched nothing.

txtNo.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent ke) {
            Connection conn = null;
            try{
                Class.forName("oracle.jdbc.driver.OracleDriver");

        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "Username", "Password");
        Statement st = conn.createStatement();
        String load = "Select * from Store_info_table where PART_NUMBER = '" + txtNo.getText() + "'";
        ResultSet rs = st.executeQuery(load);
        while(rs.next()){
            txtName.setText(rs.getString("SPARE_DESC"));
        }
            }catch(Exception ae){

            }
        }
    });

推荐答案

你知道你的数据库连接是否正常工作吗?例如,您能否在侦听器之外运行数据库部分并且它可以工作?

Do you know if you're database connection is working? For example, can you run the database portion outside of the listener and it works?

如果是这样,我建议改用 ActionListenerFocusListener.KeyListeners(虽然有时是必要的)通常很笨拙 - 通常有更好的方法(参见 Java Swing:使用 ActionMap 为和解释):

If so, I would suggest using ActionListeneror a FocusListener instead. KeyListeners (while sometimes necessary) are generally clumsy - there's usually a better approach (see Java Swing: Using ActionMap for and explaination):

import java.awt.event.*;
import javax.swing.*;

public class Example extends Box{

    JLabel txtName = new JLabel("Nothing Entered");

    public temp(){
        super(BoxLayout.Y_AXIS);
        // Add FocusListener to first field
        final JTextField txtNo = new JTextField(20);
        txtNo.addFocusListener(new CustomFocusListener(txtNo));
        add(txtNo);

        // Add TextListener to first field
        final JTextField txtNo2 = new JTextField(20);
        txtNo2.addFocusListener(new CustomFocusListener(txtNo2));
        add(txtNo2);

        // Add TextListener to first field
        final JTextField txtNo3 = new JTextField(20);
        txtNo3.addFocusListener(new CustomFocusListener(txtNo3));
        add(txtNo3);

        add(new JButton("Do Something"));

        add(txtName);

    }


    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Example());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /**
     * Your custom focust listener that does all your SQL Work
     */
    public class CustomFocusListener extends FocusAdapter{
        JTextField field;

        public CustomFocusListener(JTextField field){
            this.field = field;
        }

        @Override
        public void focusLost(FocusEvent e) {
            //Assuming your database connection works, this gives you an example to follow
            txtName.setText(field.getText());
            /*Connection conn = null;
            try{
                Class.forName("oracle.jdbc.driver.OracleDriver");

                conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "Username", "Password");
                Statement st = conn.createStatement();
                String load = "Select * from Store_info_table where PART_NUMBER = '" + field.getText() + "'";
                ResultSet rs = st.executeQuery(load);
                while(rs.next()){
                   txtName.setText(rs.getString("SPARE_DESC"));
                }
            }catch(Exception ae){

            }*/
        }
    }
}

这篇关于当输入一个 Jtextfield 时,从数据库中获取数据到 Jtextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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