Flyingearl
Flyingearl

Reputation: 679

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. Now I can make the result appear in a JTable but I can't seem to get it into a 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. But if anybody knows how I might get the result into a JTextField that would be great.

Thank you in advance.

Upvotes: 0

Views: 366

Answers (2)

Flyingearl
Flyingearl

Reputation: 679

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.

Thank you again.

Upvotes: 0

Subba
Subba

Reputation: 406

JTextField is extending JTextComponent. So calling the setText will display the corresponding text for you.

Upvotes: 1

Related Questions