Java: Help constructing a fillTextFields() method
I have a Java project where I am to connect to a database and create buttons (next, new, save, delete, previous) to navigate through the database content where there are appropriate text fields and labels for the specific information.
I'll use the code below as an example (each button is set up very similar)... I have it as follows:
JButton jbtnNext = new JButton("Next ->");
jbtnNext.addActionListener(this);
if (e.getSource() == jbtnNext) jbtnNext_Click();
private void jbtnNext_Click() {
JOptionPane.showMessageDialog(null, "Next" ,"Button Pressed",
JOptionPane.INFORMATION_MESSAGE);
try {
if (rset.next()) {
fillTextFields(true);
}else{
//Display result in a dialog box
JOptionPane.showMessageDialog(null, "Not found");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
The professor gave the following outline of logic to construct the fillTextFields()
method:
Construct the method to provide reusable code that would fill the JTextFields on the GUI with appropriate values from current record from the database (when "Previous or "Next" buttons are pressed) or blank values (when the new Button is pressed).
To determine when the current record was to provide values (next and previous) or the value would be blank (new button) pass a boolean argument into the method. If data from the current record was to be used as fill values, pass true for both previous and next button code after moving the record pointer. If the new button was pressed and want to fill with blank values, pass false to the method.
Inside the method, use a conditional expression to evaluate the boolean variable. If true, The appropriate get----() resultset method is used to fill the JTextFields. If false, fill them with "".
The .setText() method of the JTextField is used to fill each JTextField.
Make sur开发者_StackOverflow中文版e the fillTextFields method throws the appropriate exception.
I understand and have the previous and next button methods passing true, while the new button method is passing false, but I don't quite understand how to set up the fillTextFields()
method correctly or how to "throw the appropriate exception"... Any help would really be appreciated, thank you!
The fillTextFields
method, should, as the name suggest, fills textfields with data you get from the database. I presume that the rset
is a global variable, so you should be able to access it from other methods within the same class.
You can check out this tutorial so that you can get an idea of how to use the textfields in Java.
In the fillTextFields
method, you first check if the boolean that has been passed is true
or false
, if it is true
, you extract the data from the result set and use the .setText(textToPrint)
to show the data you have retrieved from the database.
To retrieve data, you can do as follows:
rset.getString(1)
The above returns the value stored in the 1st column of the database as a String. You can read through the JavaDoc to see how to return different types.
With regards to the exception to be thrown, you can check the link to the JavaDoc provided to see which methods throw what exceptions.
I don't quite understand how to set up the
fillTextFields()
method correctly or how to "throw the appropriate exception"..
JDBC methods can throw SQLException
when the DB interaction fails. The fillTextFields()
should not catch it, but just let it pass through. It will be handled in the catch (SQLException e)
you already have there in the posted code. You need to add a throws
clause with this exception to the fillTextFields()
method. E.g.
public void fillTextFields(boolean blank) throws SQLException {
Connection connection = null;
// ...
try {
connection = DriverManager.getConnection(url, username, password);
// ...
} finally {
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
// ...
}
}
See also the Sun tutorial on Exceptions and the Sun JDBC tutorial.
精彩评论