Java使用JDBC的个人bug

这是一篇关于JDBC使用中个人出现的一些问题

脚本SQL问题

对于字符串的脚本

error:java.sql.SQLSyntaxErrorException: 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 '','飞科科技有限公司',50,'剃须不残留',1)' at line 1

//对于varchar类型的需要添加”''

例如String companyName = brand.getCompanyName();

在SQL中使用companyName时需要添加''

…..+"'"+ brandName +"'" +","

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void addBrand(Brand brand) throws SQLException {
int id = brand.getId();
String brandName = brand.getBrandName();
String companyName = brand.getCompanyName();
int ordered = brand.getOrdered();
String description = brand.getDescription();
int status = brand.getStatus();
brand.toString();

sql = "insert into tb_brand (id, brand_name, company_name, ordered, description, status) values("
+ id + ","
+"'"+ brandName +"'" + "," //对于varchar类型的需要添加"''"
+"'"+ companyName +"'" + ","
+ ordered + ","
+"'"+ description +"'" + ","
+ status + ")"

添加数据脚本出错

改了好久,一直提示说第一个参数未给定,尝试了很多种办法,最后发现是前面的代码重新初始化了一个PrepareStatement对象

image-20240505180529100