ghassen92
ghassen92

Reputation: 305

PostgreSQL can't find column

I create a database that contains 4 column (ID_PRODUIT,NOM_PRODUIT,QUANTITE, PRIX).I tried to make a methods that allow me to serach Product(my class)using a string key,however, the query failed to identify the "NOM_PRODUIT" column and it turn "NOM_PRODUIT" to "nom_produit" in the messsage error.

     Connection conn = DriverManager.getConnection(url, user, passwd);

     PreparedStatement ps = conn.prepareStatement("SELECT * FrOM produits where NOM_PRODUIT like ?");

     ps.setString(1,"%"+mc+"%");
     ResultSet rs = ps.executeQuery();
     while(rs.next()){
         Produit p=new Produit();System.out.println(rs.getString(2));
         p.setIdProduit(rs.getInt(1));
         p.setNomProduit(rs.getString(2));
         p.setQuantite(rs.getInt(3));
         p.setPrix(rs.getInt(4));

     }
     ps.close();
     conn.close();

I get this error :

  org.postgresql.util.PSQLException: ERREUR: la colonne « nom_produit » n'existe pas

Here's my database.enter image description here

Upvotes: 0

Views: 2244

Answers (2)

Trinimon
Trinimon

Reputation: 13967

Just in case the issue it related to upper and lower case in the column name: it's possible to put the column name in double quotes:

 PreparedStatement ps = 
    conn.prepareStatement("SELECT * FROM produits where \"NOM_PRODUIT\" like ?");

This way the name is case sensitive.

Upvotes: 1

isma3l
isma3l

Reputation: 3853

Here

 ps.setString(1,"%"+mc+"%");

you are missing single quote ', try

ps.setString(1,"'%"+mc+"%'");

Upvotes: 0

Related Questions