String strSQL="insert into bookbrowse(StudentName,ISBN,BorrowedDate,Com)values("+BorrowedBookStudentTextField.getText()+","+ISBNComboBox+","+BorrowedDateTextField.getText()+","+BorrowedCommentTextField.getText())";
时间: 2023-07-10 17:36:34 浏览: 99
X++直接操作AX数据库.docx
这段代码存在 SQL 注入的风险,建议使用 PreparedStatement 来构建 SQL 语句,可以避免 SQL 注入攻击。同时,需要对插入的值进行转义,保证 SQL 语句的正确性。建议改为如下代码:
```java
String insertSql = "INSERT INTO bookbrowse (StudentName, ISBN, BorrowedDate, Com) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insertSql);
pstmt.setString(1, BorrowedBookStudentTextField.getText());
pstmt.setString(2, ISBNComboBox.getSelectedItem().toString());
pstmt.setString(3, BorrowedDateTextField.getText());
pstmt.setString(4, BorrowedCommentTextField.getText());
pstmt.executeUpdate();
```
这样可以有效避免 SQL 注入攻击,并且确保 SQL 语句的正确性。
阅读全文