简体   繁体   中英

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)

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.

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". The names of columns/tables in DB are all in uppercase, however the Java application generates sql statements with lowercase columns and tables

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

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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