【问题标题】:Retrieve values from database and show in textfield [closed]从数据库中检索值并显示在文本字段中[关闭]
【发布时间】:2012-12-27 23:05:28
【问题描述】:
   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);
        }

您看到无法获取并将值放回文本字段...

【问题讨论】:

  • 请帮帮家伙...!! :-(
  • 您遇到的具体问题是什么?写SQL?界面的一部分?我看到了插入代码;你对搜索功能有什么尝试?
  • 我用过...// * CallableStatement stmt = con.callableStatement("SELECT (rm_id,SUM(box_no),SUM(weight),SUM(TP),SUM(TV) FROM dbo. mut_det WHERE(rm_id=?); * // 但它没有工作
  • 我得到了你想要做的,但我没有得到你遇到问题的地方?还有一件事不要将您的代码放在评论框中。它在这里变得不可读。
  • 哦,对不起.. :-( 我想从文本字段中获取 rm_id 的值:tf_rm_id 并且我想将调用的记录显示到我的另一个文本字段但不知道这样做因此我请求帮助..

标签: java database jdbc


【解决方案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);
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2014-05-10
    • 1970-01-01
    相关资源
    最近更新 更多