String strSQL="insert into bookbrowse(StudentName,ISBN,BorrowedDate,Com)values("+BorrowedBookStudentTextField.getText()+","+ISBNComboBox+","+BorrowedDateTextField.getText()+","+BorrowedCommentTextField.getText())";
时间: 2023-07-10 11:36:34 浏览: 44
这段代码存在 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 语句的正确性。
相关问题
String strSQL="update books set bookName="+BookNameTextField.setText(rs.getString(2))+",PressName ="+PressNameTextField.setText(rs.getString(3))+",Author="+AuthorTextField.setText(rs.getString(4))+",Address="+AddressTextField.setText(rs.getString(5))+",PressDate="+PressDateTextField.setText(rs.getString(6))+",Price="+PriceTextField.setText(rs.getString(7))+",Comment="+CommentTextField.setText(rs.getString(8))+"where ISBN="+BookISBNTextField.getText().trim();
这段代码存在一些问题,应该进行如下修改:
```
String strSQL = "UPDATE books SET bookName='" + BookNameTextField.getText() + "', PressName='" + PressNameTextField.getText() + "', Author='" + AuthorTextField.getText() + "', Address='" + AddressTextField.getText() + "', PressDate='" + PressDateTextField.getText() + "', Price='" + PriceTextField.getText() + "', Comment='" + CommentTextField.getText() + "' WHERE ISBN='" + BookISBNTextField.getText().trim() + "'";
```
在这个修改后的语句中,我们使用了单引号将字符串值括起来,以保证 SQL 语句的正确性。同时,我们还使用了 getText() 方法获取文本框中的值,而不是 setText() 方法将值赋给文本框。最后,我们将 WHERE 条件中的 ISBN 值加上了单引号。
需要注意的是,这段代码中存在 SQL 注入的风险,建议使用 PreparedStatement 对象来执行 SQL 语句,以提高程序的安全性。
String strSQL="insert into books(ISBN,bookname,press,author,address,pressDate,price,books_count,com) 完善代码
好的,下面是完善代码的步骤:
1. 首先,需要确定数据库表的结构,确保表中包含了所有的字段。
2. 在 Java 中,需要通过 JDBC 连接数据库并执行 SQL 语句。可以使用如下代码获取数据库连接:
```
Connection conn = DriverManager.getConnection(url, username, password);
```
其中,`url` 是数据库的连接字符串,`username` 和 `password` 是登录数据库所需要的用户名和密码。
3. 构造 SQL 语句,可以使用如下代码:
```
String strSQL = "insert into books(ISBN, bookname, press, author, address, pressDate, price, books_count, com) " +
"values('" + ISBN + "','" + bookname + "','" + press + "','" + author + "','" + address + "','" +
pressDate + "'," + price + "," + books_count + ",'" + com + "')";
```
其中,`ISBN`、`bookname`、`press`、`author`、`address`、`pressDate`、`price`、`books_count` 和 `com` 是要插入到数据库中的值。
4. 执行 SQL 语句,可以使用如下代码:
```
Statement stmt = conn.createStatement();
int result = stmt.executeUpdate(strSQL);
```
执行 SQL 语句后,可以获得受影响的行数。
5. 最后,需要关闭数据库连接和语句对象,可以使用如下代码:
```
stmt.close();
conn.close();
```
完整代码如下:
```
String strSQL = "insert into books(ISBN, bookname, press, author, address, pressDate, price, books_count, com) " +
"values('" + ISBN + "','" + bookname + "','" + press + "','" + author + "','" + address + "','" +
pressDate + "'," + price + "," + books_count + ",'" + com + "')";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
int result = stmt.executeUpdate(strSQL);
stmt.close();
conn.close();
```
希望这个答案能够帮到你。