[JAVA]Statement与PreparedStatement
Statement和PreparedStatement都是用来执行sql语句的,那我们在使用的时候选择谁呢?
Statement
public class JdbcDemo {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8",
"root", "1579886070");
statement = connection.createStatement();
String sql = "insert into student values(null,"+"'小信'"+","+"18)";
statement.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if(statement != null){
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
PreparedStatement
public class JdbcDemo {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8",
"root", "1579886070");
String sql = "insert into student values(null,?,?)";
preparedStatement = connection.prepareStatement(sql);
//设置参数值
preparedStatement.setString(1,"小天");
preparedStatement.setInt(2,20);
//执行
preparedStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
以上分别用Statement和PreparedStatement实现数据库插入数据的案例。
对比明显总结:
Statement使用繁琐的字符串拼接,不但不易阅读,当字段比较多时容易出错。可读性与维护性都不好;
PreparedStatement使用设置参数的形式,简单易读,不易出错。
其他优点:PreparedStatement执行效率高于Statement,假设一次要插入10条数据,那么Statement需要执行10次,把10次全部传输到数据库端。
PreparedStatement防止Sql注入。
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xiaoxin1218@qq.com
文章标题:[JAVA]Statement与PreparedStatement
文章字数:344
本文作者:周信
发布时间:2019-10-16, 18:43:08
最后更新:2023-05-03, 10:25:35
原始链接:http://zx21.xyz/2019/10/16/JAVA-Statement与PreparedStatement/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。