问题
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);
回答1:
The ? is the bind variable placeholder. you only have 1. so why are you trying to bind 4 things?
回答2:
String sql = "SELECT MockID, Subject, Year, Date FROM mockexam WHERE MockID =?";
PreparedStatement prest = con.prepareStatement(sql);
prest.setString(1, "MockID");
prest.setString(2, "Subject");
prest.setString(3, "Year");
prest.setString(4, "Date");
This is the problem. In your sql string you made room for 1 argument (indicated by the ?
) but then you tried to set 4. Hence you went of out bounds. Also since you hard coded the MockID, Subject, Year, and Date into the statement there is no reason to use setString
to try set them (incorrectly).
To display the answer in your do this:
ResultSet rs = prest.executeQuery();
String result;
while(rs.next()) {
String id = rs.getString("MockID");
String subject = rs.getString("Subject");
String year = rs.getString("Year");
String Date = rs.getString("Date");
result += id + ", " + subject + ", " + year + ", " + date + "\n";
}
textarea.append(result);
回答3:
Your PreparedStatement
only has one variable..i.e MockID
SELECT MockID, Subject, Year, Date FROM mockexam WHERE MockID =?....so following statements
prest.setString(2, "Subject");
prest.setString(3, "Year");
prest.setString(4, "Date");
are not required.
Update for your comments
:
we do setString
for all the variables which are represented by "?"....and here only MockId is a variable.....you can retrieve the whole row if you want by using "select *"...but still there will be only one argument(represented by ?) to PreparedStatement
and that is your MockId
.
回答4:
"Date" is not a good choice for a column name. It's a reserved word in every database I know of. It's not very informative for users, either. Birth date? Death date? Order date? What does it mean?
I'd start by changing to "EXAM_DATE" or something else and see if that helps.
回答5:
String sql = "SELECT MockID, Subject, Year, Date FROM mockexam WHERE MockID =?";
When preparing this statement, you "say" that you will give a value for each ?
.
Here you only have one but you define 4 of them later on :
PreparedStatement prest = con.prepareStatement(sql);
prest.setString(1, "MockID");
prest.setString(2, "Subject");
prest.setString(3, "Year");
prest.setString(4, "Date");
And don't forget to close your connection, statement and possible result set to avoid memory leaks.
来源:https://stackoverflow.com/questions/9621428/java-mysql-preparedstatement-select-statement