English 中文(简体)
将SQL和绑定数据转换为直接SQL
原标题:convert SQL and binding data to straight SQL

我有一个自定义的Java查询引擎,它生成一个SQL字符串(以?作为数据占位符)和一个包含要绑定到占位符的数据的对象列表。然后将其传递给JDBCTemplate.queryForList(sql,bindings.toArray())API以生成结果集。

这很好,但我也需要在子查询中使用此SQL,并且无法找到一种(简单的方法)将其转换为直接SQL(使用Java类型和数据库友好格式之间的适当数据库转换等)。

有实用程序可以做到这一点吗?

最佳回答

You might want to look at the following utility.
http://commons.apache.org/dbutils/

虽然这不会直接回答你的问题,但它可能在未来有用。我也遇到过这个问题,我设计了一种对我有效的方法,尽管不是最好的。我创建了一个方法,按照您的建议自动填充参数中的值,但它根本没有以类型安全的方式完成。无论如何,如果你决定使用这种方法,也许你可以采取这种方法并加以改进:

public static String preparedQueryString(String staticQuery, Object... parameters) {
    for(Object curParameter : parameters) {
        if(curParameter instanceof String) {
            staticQuery = staticQuery.replaceFirst("\?", " " + curParameter.toString() + " ");
        } else {
            staticQuery = staticQuery.replaceFirst("\?", curParameter.toString());
        }

    return staticQuery;
}

您可能还想添加代码以确保参数的数量不会超过问号的数量,并且您可能还希望使java类型更适合转换为您使用的数据库。我用它来输出PreparedStatement的SQL,因为PreparedSStatement没有任何好的功能,可以在设置适当的参数后提取完成的SQL查询。

希望这能有所帮助,

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签