从数据库中检索值并在textfield中显示

时间:2012-12-27 23:05:29

标签: java database jdbc

   DBUtil util = new DBUtil();
        try {
            JOptionPane.showMessageDialog(null, "Connection Opened!");
            Connection con = util.getConnection();
            PreparedStatement stmt = con.prepareStatement("SELECT (SUM(box_no),SUM(weight),SUM(TP),SUM(TV)) FROM dbo.mut_det WHERE rm_id=?");
            stmt.setInt(1, Integer.parseInt(rm));// but how to get values from textfield dont know.
            //*and how to put these called values SUM(box_no),SUM(weight),SUM(TP),SUM(TV) in textfields dnt know.*//
            //my textfields are txtboxno, txtweight, txttp, txttv
            stmt.execute();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
        }

您认为无法获取并将值重新放回文本字段...

1 个答案:

答案 0 :(得分:1)

以下是可能有助于解决问题的脏样本代码。

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

public class TextDemo extends JFrame implements ActionListener {
    JTextField textData;
    JButton button = new JButton("Press Me");

    public TextDemo() {
        JPanel myPanel = new JPanel();
        add(myPanel);
        myPanel.setLayout(new GridLayout(3, 2));
        myPanel.add(button);
        button.addActionListener(this);
        textData = new JTextField();
        myPanel.add(textData);
    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            String data = textData.getText(); // perform your DB operation
            textData.setText(data + " This is new text"); // Set your DB values.
        }
    }

    public static void main(String args[]) {
        TextDemo g = new TextDemo();
        g.setLocation(10, 10);
        g.setSize(300, 300);
        g.setVisible(true);
    }
}