为什么PreparedStatement比Statement更可取 [英] Why PreparedStatement is preferable over Statement

查看:109
本文介绍了为什么PreparedStatement比Statement更可取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管,PreparedStatement可以执行所有操作,即与DDL,DML相关的查询,其性能要优于Statement.Than,为什么Java API中仍然存在Statement接口?

Though , PreparedStatement can do everything i.e DDL ,DML related queries with better performance than Statement.Than , why still Statement interface exist in Java API?

推荐答案

因为某些查询没有任何参数,所以准备语句然后执行它实际上要比直接执行语句效率低.在这种情况下,使用(并可能重用)单个语句也更易读和直接.

Because some queries don't have any parameter, and preparing the statement and then executing it can actually be less efficient than executing the statement directly. It's also more readable and straightforward in this case to use (and potentially reuse) a single statement.

以下是 javadoc的内容说,顺便说一句:

Here's what the javadoc says, BTW:

不带参数的SQL语句通常使用 语句对象.如果同一条SQL语句多次执行, 使用PreparedStatement对象可能会更有效.

SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.

这篇关于为什么PreparedStatement比Statement更可取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆