How to Solve Error Message null: org.postgresql.util.PSQLException: ERROR: column “names” of relation “employee” does not exist

Posted on

Introduction

This is also an article where the content available in it have a connection with the previous article. Actually, that article has the title of ‘How to Solve Error Message null: org.postgresql.util.PSQLException: No value specified for parameter 1 when running Java Web Application using Primefaces Framework’ in this link. There is an error upon executing a submit button in the form available in a JSF file. That JSF file is a part of a Java web application using Primefaces framework running in a Wildfly Application Server. In order to run it, build, compile and deployment process is possible using NetBeans IDE. So, after the form submission, the Output tab of the NetBeans IDE will show the following error message :

12:28:11,695 SEVERE [com.mycompany.web.view.BasicInputTextDBView] (default task-1) null: org.postgresql.util.PSQLException: ERROR: column "names" of relation "employee" does not exist
  Position: 22
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675)
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2365)
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:355)
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:490)
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.jdbc.PgStatement.execute(PgStatement.java:408)
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:167)
    at deployment.app-1.0-SNAPSHOT.war//org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:156)
    at deployment.app-1.0-SNAPSHOT.war//id.go.djpk.training.web.view.BasicInputTextDBView.submit(BasicInputTextDBView.java:74)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)

Before getting on to the solution, the following is the several files and scripts causing the error message. First of all, it is the JSF file where the form exist :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
      <head>
            <title>TODO supply a title</title>
            <meta name="viewport" content="width-device-width, initial-scale=1.0"> 
      </head>
      <body>
            <div>TODO content</div>
            <div class="card">
                 <h:form>
                 <h5>Form User with only Basic InputText</h5>
                 <h5>Hello, <p:outputLabel for="name" value="#{basicInputTextDBView.name}"></p:outputLabel></h5>
                 <p:inputText id="name" value="#{basicInputTextDBView.name}"/>
                 <p:commandButton actionListener="#{basicInputTextDBView.submit()}" value="Submit" />
                 </h:form> 
            </div>
            <div class="card">
                 <p:panel header="User List">
                    <p:dataTable var="user" value="#{userBean.userList}">
                        <p:column>
                           <f:facet name="header">
                              <h:outputText value="Username" />  
                           </f:facet>
                           <h:outputText value="#{user.name}" />  
                        </p:column>
                    </p:dataTable>
                 </p>
            </div>
      </body>
</html>

As in the above JSF file script exist, there is an element of inputText with the id of ‘username’. It will be the source for getting the value for further execution by inserting it into a table using a specific query. The process for doing that exist in a backing bean in the form of a Java file with the name of ‘BasicInputTextDBView.java’. In order to describe it in detail, the following is the content of that Java file :

package com.mycompany.web.view;

import com.mycompany.web.bean.UserBean;
import com.mycompany.web.db.DBConnection;
import com.mycompany.web.model.User;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

/**
 *
 * @author Mark Spectre
 */
@Named(value = "basicInputTextDBView")
@ViewScoped
public class BasicInputTextDBView implements Serializable {


    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private final String TABLE_NAME = "employee";
    private User user;
    private List userList; 
    
    private String name;
    
    public BasicInputTextDBView() {
        connection = DBConnection.getConnection();
        user = new User();
        userList = user.getUserList();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }  
    
    public void submit() {
        try {
            String query = "INSERT INTO " + TABLE_NAME
                    + "(names) "
                    + " values(?)";
            System.out.println(query);   
            System.out.println("Name : "+name);
            connection = DBConnection.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            preparedStatement.execute();
            preparedStatement.close();
            preparedStatement.setString(1, name); //getName());
            User user = new User();
            user.setUsername(name);
            userList.add(user);
        } catch (SQLException ex) {
            Logger.getLogger(BasicInputTextDBView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Solving the Error Message

Actually, it is very easy to solve the problem above. The reason is because of the incorrect or misstyped declaration of the column name in the query declaration variable. It exist in the Java file which the specific script exist as follows :

String query = "INSERT INTO " + TABLE_NAME + "(names) " + " values(?)";

There is no column with the name of ‘names’ in the ’employee’ table where it is the value of the ‘TABLE_NAME’ constant. In order to confirm it, just access the database and check the actual column name. In this context, this article is using PostgreSQL database, so the following is the actual execution of it :

Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Personal>psql -Upostgres
Password for user postgres:
psql (14.2)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.

postgres=# \c employee;
You are now connected to database "employee" as user "postgres".

employee=# \d+ employee;
Table "public.employee"
Column  | Type              | Collation | Nullable | Default                              | Storage  | Compression | Stats target | Description
--------+-------------------+-----------+----------+--------------------------------------+----------+-------------+--------------+-------------
id      | integer           |           | not null | nextval('employee_id_seq'::regclass) | plain    |             |              |
name    | character varying |           | not null |                                      | extended |             |              |
Indexes:
"employee_pkey" PRIMARY KEY, btree (id)
Access method: heap

employee=#

According to the output of the above PostgreSQL database execution command, there is no column with the name of ‘names’ in the table ’employee’. So, in order to solve the error message, just change the column name in the query declaration containing ‘names’ to ‘name’ in the backing bean of the Java file with the name of ‘BasicInputTextDBView.java’ as follows :

String query = "INSERT INTO " + TABLE_NAME + "(name) " + " values(?)";

Leave a Reply