简体   繁体   English

使用mysql变量到Java JDBC准备语句中

[英]Using mysql variables into a java JDBC prepared statement

let's say I have the next query: 假设我有下一个查询:

SELECT UNIX_TIMESTAMP(Logins.FechaLogin) FROM GA.Logins WHERE Logins.IdEmpleado = ? AND UNIX_TIMESTAMP(Logins.FechaLogin) >= UNIX_TIMESTAMP(?) AND UNIX_TIMESTAMP(Logins.FechaLogin) <= UNIX_TIMESTAMP(?)

And I want something like: 我想要这样的东西:

Date = UNIX_TIMESTAMP(Logins.FechaLogin);

SELECT UNIX_TIMESTAMP(Logins.FechaLogin) FROM GA.Logins WHERE Logins.IdEmpleado = ? AND Date >= UNIX_TIMESTAMP(?) AND Date <= UNIX_TIMESTAMP(?)

stmt.setInt(1, EmployeeId);
stmt.setString(2, Date1);
stmt.setString(3, Date2);

All into a Prepared Statement (JDBC prepareStatement()), is there a way I could do something like this in order to avoid redundance into the query? 所有这些都放入了一条Prepared语句(JDBC prepareStatement())中,有没有一种方法可以避免这种冗余,从而避免查询重复? or is this something useless? 还是这没用? by the way I think it has to pass the UNIX_TIMESTAMP function in each SELECT iteration if I'm correct. 顺便说一句,如果我是对的,我认为它必须在每次SELECT迭代中都传递UNIX_TIMESTAMP函数。

Thank's! 谢谢!

The short answer is no, you can't reference a MySQL variable in place of a column reference, or in place of an expression that contains column references. 简短的回答是“否”,您不能引用MySQL变量来代替列引用,也不能代替包含列引用的表达式。 In a prepared statement, you can only "bind" variables to values; 在准备好的语句中,您只能将变量“绑定”到值。 you can't "bind" table names or column names. 您不能“绑定”表名或列名。

(This restriction has to do with the way a SQL statement is processed. When a SQL statement is "prepared", the optimizer needs to know all of the tables and columns that are being referenced. It has to verify that the references are valid, that the user has privileges on the referenced objects, it has to lookup datatypes of the columns, check for suitable indexes, and so on. The issue is that it can't do that if column names are missing. This explains why you won't find any examples of how to do what you are wanting to do.) (此限制与处理SQL语句的方式有关。“准备” SQL语句时,优化器需要知道所有被引用的表和列。它必须验证引用是否有效,用户具有对引用对象的特权,必须查找列的数据类型,检查合适的索引,等等。问题是,如果缺少列名,则无法执行此操作。这说明了为什么您不会找不到有关如何做自己想做的事的例子。)

Obviously, you could make use of Java variables and string manipulation to produce a String containing the SQL text that will be passed to MySQL. 显然,您可以利用Java变量和字符串操作来生成一个包含要传递给MySQL的SQL文本的String。 But the final string that gets passed to MySQL can have placeholders, but those can only be placeholders for values. 但是传递给MySQL的最后一个字符串可以具有占位符,但是它们只能是值的占位符。

That should answer your question. 那应该回答你的问题。


There's several other notes we can make, regarding the use of the UNIX_TIMESTAMP function (whether that's even necessary or desirable), performance implications of predicates that have column references wrapped in functions, etc. But those don't really answer the question you were asking. 关于UNIX_TIMESTAMP函数的使用,我们还可以做一些其他说明(无论是必要的还是期望的),将列引用包裹在函数中的谓词对性能的影响等。但是这些并不能真正回答您所问的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM