Java: Writing SQL Statements

前端 未结 4 992
旧时难觅i
旧时难觅i 2021-01-20 23:18

I\'m writing a one-time Java program to add a bunch of rows in a CSV file to a MySQL database. Are there any Java classes/toolkits to help with this? Something that will escape

4条回答
  •  攒了一身酷
    2021-01-20 23:51

    This might be considered a bit of a sledgehammer approach, but you might want to considering using spring for your SQL calls, then the above becomes as simple as:

    getSimpleJdbcTemplate().update(
    "INSERT INTO node (type, language, title) VALUES (?, ?, ?)", 
    node.get("type"), 
    node.get("language"), 
    node.get("title"));
    

    This has the advantage of using JDBC prepared statments under the hood, so you won't end up in trouble if the title includes quotes or other characters that would otherwise have to be escaped, while letting Spring handle all the connection, prepared statement and transaction (if needed at all) complexity.

    For more see: SimpleJdbcTemplate from the Spring Framework

提交回复
热议问题