0

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

Zoltan Ersek
  • 755
  • 9
  • 28
  • 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 Apr 05 '16 at 11:55
  • Can you not change your *database* collation to be case-insensitive, but have your columns with a case-sensitive collation if necessary? – Matt Gibson Apr 05 '16 at 12:32

3 Answers3

1

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.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

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

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

M.Gwozdz
  • 129
  • 1
  • 2
  • 10