Thread: Problem using PostgreSQL 9.0.4 with Java

Problem using PostgreSQL 9.0.4 with Java

From
Arun Nadar
Date:
Hi

I am currently using 'PostgreSQL 9.0.4' database with JDBC driver 'postgresql-9.0-801.jdbc4'. In my Java program normal SELECT query didn't work.
ie, 
try {
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection("jdbc:postgresql://localhost/Student","postgres","postgres");
            statement = connection.createStatement();
            String sql="SELECT Id, Name FROM Student ORDER BY Id";     // problem
            resultSet = statement.executeQuery(sql);
            if(resultSet.next()) {
                String id=resultSet.getString(1);
                String name=resultSet.getString(2);
            }   
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

I directly write a query via pgAdmin III the above SELECT query didn't work
{ SELECT Id, Name FROM Student ORDER BY Id; }

but it work by putting " ",  like this SELECT "Id", "Name" FROM "Student" ORDER BY "Id";

in java String,  inside of double inverted commas another is does not possible.

how this code is implement through java program. please kindly send me the solution for this.
 
Thanks & Regards
Arun.R.T

Re: Problem using PostgreSQL 9.0.4 with Java

From
"Tomas Vondra"
Date:
On 7 Září 2011, 22:45, Arun Nadar wrote:
> Hi
>
>
> I am currently using 'PostgreSQL
> 9.0.4' database with JDBC driver 'postgresql-9.0-801.jdbc4'. In my Java
> program normal SELECT query didn't work.
> ie, 
>
> try {
>             Class.forName("org.postgresql.Driver");
>             connection =
> DriverManager.getConnection("jdbc:postgresql://localhost/Student","postgres","postgres");
>             statement = connection.createStatement();
>             String sql="SELECT Id, Name FROM Student ORDER BY Id";     //
> problem
>
>             resultSet = statement.executeQuery(sql);
>             if(resultSet.next()) {
>                 String id=resultSet.getString(1);
>                 String name=resultSet.getString(2);
>             }   
>         } catch (Exception e) {
>             e.printStackTrace();
>         } finally {
>             try {
>    
>              connection.close();
>             } catch (Exception e) {
>                 e.printStackTrace();
>             }
>         }
>
>
> I directly write a query via pgAdmin III the above SELECT query didn't
> work
> { SELECT Id, Name FROM Student ORDER BY Id; }
>
>
> but it work by putting " ",  like this  SELECT "Id", "Name" FROM "Student"
> ORDER BY "Id";
>
> in java String,  inside of double inverted commas another is does not
> possible.
>
>
> how this code is implement through java program. please kindly send me the
> solution for this.

First of all, this has nothing to do with Java - this is caused by quoting
the identifiers when creating the table. I.e. you've created the table
like this:

CREATE TABLE "Student" (
  "Id" ...
  "Name" ...
);

in that case you have to quote the identifiers every time you use them. I
generally don't recommend it, in my experience it makes the db difficult
to use and error prone.

CREATE TABLE student (
  Id ...
  Name ...
);

and then you don't need the quotes at all.

But if you really need to use the quotes (e.g. if you can't change the
schema), then you can escape them in the query string, e.g. like this:

String sql="SELECT \"Id\", \"Name\" FROM \"Student\" ORDER BY \"Id\"";

regards
Tomas


Re: Problem using PostgreSQL 9.0.4 with Java

From
John R Pierce
Date:
On 09/07/11 1:45 PM, Arun Nadar wrote:
>
> but it work by putting " ",  like this SELECT "Id", "Name" FROM
> "Student" ORDER BY "Id";
>
> in java String,  inside of double inverted commas another is does not
> possible.


String sql="SELECT \"Id\", \"Name\" FROM \"Student\" ORDER BY \"Id\"";

Alternately, create your table with all lower case names, and you won't
have this problem.

     alter table "Student" rename to student;
     alter table student rename column "Id" to id;
     alter table student rename column "Name" to name;



--
john r pierce                            N 37, W 122
santa cruz ca                         mid-left coast


Re: Problem using PostgreSQL 9.0.4 with Java

From
"David Johnston"
Date:

From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Arun Nadar
Sent: Wednesday, September 07, 2011 4:45 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Problem using PostgreSQL 9.0.4 with Java

 

Hi

 

but it work by putting " ",  like this SELECT "Id", "Name" FROM "Student" ORDER BY "Id";

 

in java String,  inside of double inverted commas another is does not possible.

 

how this code is implement through java program. please kindly send me the solution for this.

 

 

To include the “quote” symbol in a Java string you need to escape it with the “back-slash” symbol (i.e.,  “\” ) as so:

 

String var = “SELECT \”Id\” FROM \”Table\””;

 

The reason you are having to do this is you defined your table and column names in a case-sensitive manner BUT PostgreSQL automatically converts to lowercase any identifier that is not enclosed in quotes.

 

I would suggest you do some more reading on how Strings in Java work since if you missed how to include the common quote in a String literal you likely missed some other important rules/behavior as well.

 

On the PostgreSQL side I do not know how you created your table(s) but unless you have some overriding reason to avoid doing so you should stick to lower-case; it will make using those tables from within Java much easier (during your hard-coded queries phase of development).  You can still use mixed-case in Java (without the quotes) and then let PostgreSQL convert it into lower-case for you.

 

David J.