0

This error happens when executing any query in a table (Clientes).

package database;
import java.sql.*;
import java.util.Properties;

// Database Access Object
public class ClienteDao {
    private Connection conexion = null;
    public void conectar () {
        String baseDeDatos = "pruebaLucas";
        String usuario = "postgres";
        String pass = "contra31";
        String host = "localhost";
        String puerto = "5432";
        String driver = "org.postgresql.Driver";
        String conexionUrl = "jdbc:postgresql://" + host + ":" + puerto + "/" + baseDeDatos +
                "?user=" + usuario + "&password=" + pass + "&ssl=false";

        try {
            Properties props = new Properties();
            props.setProperty("currentSchema", "schema_luki");
            Class.forName(driver);
            this.conexion = DriverManager.getConnection(conexionUrl, props);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private String construirQueryAgregarCliente (String cNombre, String cApellido, String cTel, String cEmail) {
        return String.format(
                "INSERT INTO Clientes (nombre, apellido, telefono, email)" + "VALUES (%s, %s, %s, %s);",
                cNombre, cApellido, cTel, cEmail);
    }

    private void ejecutarQuery (String q) {
        if (this.conexion != null) {
            try {
                Statement statement = this.conexion.createStatement();
                statement.executeQuery(q);
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public void agregarCliente (String nombre, String apellido, String telefono, String email) {
        this.ejecutarQuery( this.construirQueryAgregarCliente(nombre, apellido, telefono, email) );
    }


}

I execute agregarCliente(...) in a form (button click) and I get the error:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: org.postgresql.util.PSQLException: ERROR: relation "clientes" does not exist
  Position: 13
    at database.ClienteDao.ejecutarQuery(ClienteDao.java:41)
    at database.ClienteDao.agregarCliente(ClienteDao.java:48)
    at businessLogic.ManejadorClientes.agregarCliente(ManejadorClientes.java:15)
    at frontend.DataForm$1.actionPerformed(DataForm.java:49)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
    at java.desktop/java.awt.Component.processEvent(Component.java:6391)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

This is the structure of the Data base that I'm using (pruebaLucas):

enter image description here

How can I fix this problem? The connection look fine, It's something with the query.

Lucas David Ferrero
  • 1,630
  • 14
  • 40
  • 1
    [Possible duplicate] https://stackoverflow.com/questions/28571848/spring-boot-jpa-insert-in-table-with-uppercase-name-with-hibernate – Scary Wombat Aug 17 '22 at 00:14
  • 1
    Generally best to name things in your database in all lowercase, with underscores to separate words. This brings the most portability with other databases. And using all lowercase avoids the mixed case confusion you encountered here. The SQL spec actually requires that a database engine convert to all UPPERCASE when creating names. But I don’t know of any DB that obeys the standard on that point. Certainly Postgres does not, despite generally hewing quite closely to the SQL spec. – Basil Bourque Aug 17 '22 at 00:44
  • 1
    Bonus tip: The SQL spec explicitly promises to never name any keyword with a trailing underscore. So appending a trailing underscore to your data as names is an easy way to avoid collisions. And another tip: table names should be singular, rather than plural, if each row represents a single entity. So, putting all these tips together: `cliente_`. – Basil Bourque Aug 17 '22 at 00:47
  • Thanks for your wisdom @BasilBourque, I've renamed the table, and It works. However, the `INSERT` syntax wasn't right (each value is surrounded by `'`). This is how it looks now: `"INSERT INTO cliente_ (nombre, apellido, telefono, email) " + "VALUES ('%s', '%s', '%s', '%s');"`. Works great, I'll keep in mind the naming convention! – Lucas David Ferrero Aug 17 '22 at 15:57

0 Answers0