简体   繁体   English

将jtextfield设置为数据库结果

[英]Setting jtextfield to a database result

I've been trying to work this problem out for a while now and it seems to be eluding me. 我已经尝试解决这个问题已有一段时间了,而且似乎使我难以理解。 Now I could be missing something ever so simple and I'm sorry in advance if it is. 现在,我可能会丢失一些如此简单的东西,如果可以的话,我很抱歉。

I'm trying to display the result from an SQL sum into a JTextField. 我正在尝试将SQL总和的结果显示到JTextField中。 Now I can make the result appear in a JTable but I can't seem to get it into a JTextField. 现在,我可以使结果出现在JTable中,但似乎无法将其放入JTextField中。

The code is below: 代码如下:

String start = ((JTextField)startDate.getDateEditor().getUiComponent()).getText();
String end = ((JTextField)endDate.getDateEditor().getUiComponent()).getText();
String sql = "SELECT SUM(OD_GROSS) FROM ORD_DETAIL WHERE OD_ACCOUNT = ? AND OD_DATE BETWEEN '"+start+"' AND '"+end+"'";

pst = conn.prepareStatement(sql);
pst.setString(1, txtAccountNumber.getText());

rs = pst.executeQuery();
tblTotal.setModel(DbUtils.resultSetToTableModel(rs));

} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}

The above code does work and shows the sum in a JTable. 上面的代码确实起作用,并在JTable中显示了总和。 But if anybody knows how I might get the result into a JTextField that would be great. 但是,如果有人知道如何将结果放入JTextField中,那就太好了。

Thank you in advance. 先感谢您。

JTextField is extending JTextComponent . JTextField扩展了JTextComponent So calling the setText will display the corresponding text for you. 因此,调用setText将为您显示相应的文本。

I've managed to solve it in the end. 我已经设法解决了。 Thank you for your help. 谢谢您的帮助。 I've put the new code below: 我将新代码放在下面:

\\Changed SQL string notice the new AS Totals section

String sql = "SELECT SUM (OD_GROSS) AS Totals FROM ORD_DETAIL WHERE OD_ACCOUNT = ? AND OD_DATE BETWEEN '"+start+"' AND '"+end+"'";

I then called the new column Totals in the resultset and displayed it using setText. 然后,我在结果集中调用新列“总计”,并使用setText显示它。

Thank you again. 再次感谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM