本文介绍了Java的PreparedStatement如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算用 PreparedStatement 对象替换重复执行的Statement对象以提高性能。我使用MySQL函数 now()和字符串变量等参数。

I am planning to replace repeatedly executed Statement objects with PreparedStatement objects to improve performance. I am using arguments like the MySQL function now(), and string variables.

我见过的大多数 PreparedStatement 查询都包含常量值(例如 10 ,以及New York等字符串作为查询中用于的参数。我将如何使用 now()等函数和变量作为参数?是否有必要在查询中使用而不是实际值?我很困惑。

Most of the PreparedStatement queries I have seen contained constant values (like 10, and strings like "New York") as arguments used for the ? in the queries. How would I go about using functions like now(), and variables as arguments? Is it necessary to use the ?s in the queries instead of actual values? I am quite confounded.

推荐答案

如果你有变量,请使用'?'

If you have variables use the '?'

int temp = 75;
PreparedStatement pstmt = con.prepareStatement(
    "UPDATE test SET num = ?, due = now() ");
pstmt.setInt(1, temp); 
pstmt.executeUpdate():

生成一个sql语句,如下所示:

Produces an sql statment that looks like:

UPDATE test SET num = 75, due = now();

这篇关于Java的PreparedStatement如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 22:05