JDBC-prepareStatement-我应该如何使用它? [英] JDBC - prepareStatement - How should I use it?
问题描述
我在某个地方看到了这个例子:
I saw this example somewhere:
rs = connection.prepareStatement("select * from table").executeQuery();
如果我要执行类似"从列="hello" "的表中选择*,是否可以使用这种格式?
Could I use this format, if I want to execute a query like this "Select * from table where column = "hello" "?
我通常使用prepareStatement对象的方式如下:
The way in which I usual I use prepareStatement object is something like this:
String sql = "select * from adresa where column = ?";
PreparedStatement pre = con.prepareStatement(sql);
pre.setString(1, i);
rs = pre.executeQuery();
以后的
我不明白. Pascal Thivent写道,我可以使用带有In参数的简短版本,但是Liu告诉我这是不可能的. :) Anw,使用Pascal的版本,我收到此错误: void不能被取消引用
I don't understand. Pascal Thivent wrote that I can use the short version with In parameters, but Liu tells me this is not possible. :) Anw, using Pascal's version, i receive this error: void cannot be dereferenced
推荐答案
如果查询中没有绑定变量(问号),则只能使用第一种形式.这只是您发布内容的简化版本.
You can only use the first form if there are no bind variables (question marks) in the query. It's just a shortened version of what you posted.
此外,如果使用简化形式,则将没有机会重用PreparedStatement对象.
Also, if you use the shortened form you won't have the opportunity to reuse the PreparedStatement object.
这篇关于JDBC-prepareStatement-我应该如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!