简体   繁体   English

如何在sql语句(Java)中大写列名和表名?

[英]How do I uppercase column and table names in a sql statement(Java)?

I have a string that can hold any kind of sql statement(select, update, delete, insert) 我有一个可以容纳任何类型的sql语句的字符串(选择,更新,删除,插入)

I want to uppercase the column and table names in that statement. 我想在该语句中将列名和表名大写。

Let's say we have: 假设我们有:

select id from person where name="Dave"

And I want 而且我要

 select ID from PERSON where NAME="Dave"

Until now I have found some Sql parsers in Java, but I am wondering if there is another faster easier way that parsing the sql and rebuilding it. 到目前为止,我已经在Java中找到了一些Sql解析器,但是我想知道是否还有另一种更简单的方法来解析sql并重新构建它。

EDIT 编辑

Just to clarify the question further, the database collation is in Turkish and the problem that I am trying to solve is the "Turkish i problem". 只是为了进一步澄清问题,数据库排序是土耳其语,而我要解决的问题是“土耳其语i问题”。 The names of columns/tables in DB are all in uppercase, however the Java application generates sql statements with lowercase columns and tables DB中的列/表名称全为大写,但是Java应用程序使用小写的列和表生成sql语句

You shall use prepared statements with bind variables. 您应该使用带有绑定变量的准备好的语句。 By doing that you can uppercase your query and then put bind variables in whatever case you want. 这样,您可以大写查询,然后在任何需要的情况下放置绑定变量。

For example: 例如:

 String query = "select id from person where name=?"
 Connection con = .... ;
 PreparredStatement ps = con.prepareStatement(query.toUpperCase());
 ps.setString(1, "Dave");

 ResultSet rs = ps.executeQuery();

Hope this helps. 希望这可以帮助。

我不确定我是否正确理解了您的问题,但是如果您想以大写形式检索特定的列名,那么查询将如下所示:

SELECT id AS ID FROM person WHERE name = "Dave";

You can do something like this: 您可以执行以下操作:

public String queryText(final String message, final String... args) {
    return String.format(message.toUpperCase().replace("?", "%s") + "%n", args);
}

And call it this way: 并这样称呼:

System.out.println(queryText("select id from person where name=?", "Dave"));

Output: SELECT ID FROM PERSON WHERE NAME=Dave 输出:SELECT ID FROM PERSON WHERE NAME = Dave

Hope it helps 希望能帮助到你

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

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