0
votes

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

3
If the collation is case insensitive, then just upper case the entire query. TSQL isn't case sensitive. If not, you should be using parameters anyway, so upper case your query, then bind. - Liesel
Can you not change your database collation to be case-insensitive, but have your columns with a case-sensitive collation if necessary? - Matt Gibson

3 Answers

1
votes

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.

0
votes

I'm not sure if I understand your question correctly but if you want to retrieve the specific column name in uppercase then your query would look like,

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

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