Java,插入sql写入sql文件里
public static void main(String[] args) {
// sql存放目录
String sqlPath = "/sql/data.sql";
Map<Integer, String> map = new HashMap<>();
map.put(1, "11111111111");
// 执行sql
for(Map.Entry<Integer, String> entry : map.entrySet()){
StringBuilder str = new StringBuilder();
str.append("INSERT INTO `chapter`(`chapter_number`, `content_imges`) VALUES (");
str.append(entry.getKey()+", ");
str.append("'"+entry.getValue()+"');");
// 写入sql文档
appendSQLToFile(sqlPath, str.toString());
}
}
private static void appendSQLToFile(String filePath, String sqlStatement) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
// 换行
writer.newLine();
writer.write(sqlStatement);
writer.newLine();
System.out.println("SQL statement added to the file successfully.");
} catch (IOException e) {
System.err.println("Error occurred while writing to the file: " + e.getMessage());
}
}