I have a database and a Java Program. I am trying to write a code, so that if in a textfield a MockID is entered, and the submit button is pressed, the details according to that entered Mock ID should be retrieved from the database and displayed in a textarea. below is the code that i have written. The code now works after amendments i have made. However, in the textarea, its not actually displaying the relevant information from that record for the given Mock Id, but just text.
Could someone please advise ?
JButton button = new JButton("Submit");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String mockId = textField.getText();
try {
String sql = "SELECT MockID, Subject, Year, Date FROM mockexam WHERE MockID =?";
PreparedStatement prest = con.prepareStatement(sql);
prest.setString(1, mockId);
prest.executeQuery();
textArea.append("MockID, Subject, Year, Date");
JOptionPane.showMessageDialog(frmFindMock, "Record has been updated.");
}
catch (SQLException e) {
//System.out.println("Record couldn't be added!");
e.printStackTrace();
JOptionPane.showMessageDialog(frmFindMock, "Record couldn't be updated. Please try again.");
}
}
});
button.setBounds(303, 60, 75, 23);
panel_1.add(button);
The ? is the bind variable placeholder. you only have 1. so why are you trying to bind 4 things?