코드를 작성했습니다. 버튼을 누르면 텍스트 필드의 값을 사용하여 데이터베이스에 레코드를 만듭니다. 코드를 컴파일하지만 실행하면 다음 오류 메시지가 표시됩니다.PreparedStatement Java에 삽입
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From, To, TotalDays, VacationType, Notes, Signature, Date) VALUES('','','','',''' at line 1
제안 사항? (당신이 ANSI 모드가 활성화되어있는 경우 또는 따옴표)
final JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String vacationid = text_vacationID.getText();
String staffid = text_staffID.getText();
String from = text_from.getText();
String to = text_to.getText();
String totaldays = text_totalDays.getText();
String vacationtype = text_vacationType.getText();
String notes = textArea.getText();
String signature = text_signature.getText();
String date = text_date.getText();
String sql = "INSERT into vacation (VacationID, StaffID, From, To, TotalDays, VacationType, Notes, Signature, Date) VALUES" + "(?,?,?,?,?,?,?,?,?)";
PreparedStatement prest = con.prepareStatement(sql);
prest.setString(1, vacationid);
prest.setString(2, staffid);
prest.setString(3, from);
prest.setString(4, to);
prest.setString(5, totaldays);
prest.setString(6, vacationtype);
prest.setString(7, notes);
prest.setString(8, signature);
prest.setString(9, date);
prest.executeUpdate();
JOptionPane.showMessageDialog(frmBookVacation, "Vacation has been booked for Employee with ID: " + vacationid);
}
catch (SQLException e) {
//System.out.println("Record couldn't be added!");
e.printStackTrace();
JOptionPane.showMessageDialog(frmBookVacation, "Vacation couldn't be booked. Please try again.");
}
}
});
btnSubmit.setBounds(201, 350, 89, 23);
panel_1.add(btnSubmit);
보낸 사람은 To와 Date라는 예약어로되어 있습니다 (매뉴얼을 확인하십시오). – Eduardo