【发布时间】:2021-03-09 18:37:06
【问题描述】:
我正在尝试使用 java 中的 INSERT 语句将数据插入 MySQL 数据库。但是,我使用的变量之一包含一个撇号,我不确定如何在语句中说明它。
public static void addAlbum(Album album) throws Exception {
try{
Connection conn = getDBConnection();
PreparedStatement posted = conn.prepareStatement(
"INSERT INTO album(title, year, singer, company) VALUES ('" + album.getName() +"', '"+ album.getYear() +"', '"+ album.getSinger() +"', '"+ album.getCompany() +"')"
);
posted.executeUpdate();
}
catch(Exception e) {
System.out.println(e);
}
finally{System.out.println("Insert completed");}
}
album.getName() 检索到“我回来了”的字符串,我该怎么做才能正确插入该值,而不是用撇号切断字符串并使语句无效?
【问题讨论】: