Zoltan Ersek
Zoltan Ersek

Reputation: 775

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

Upvotes: 0

Views: 2356

Answers (3)

M.Gwozdz
M.Gwozdz

Reputation: 129

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

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 9946

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.

Upvotes: 1

user5708826
user5708826

Reputation:

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";

Upvotes: 0

Related Questions