Java PreparedStatement 函数 uuid_generate_v4() 不存在问题

Java PreparedStatement function uuid_generate_v4() does not exist issue

我是 Java 的新手,所以我希望我的问题对您来说相当简单。当我尝试使用 PreparedStatement class 插入值时,我不断收到“错误:函数 uuid_generate_v4() 不存在”。基本上在我的情况下,保存功能在我创建的 EmployeeDAL 中不起作用。

我已经 运行“如果不存在则创建扩展”uuid-ossp;”并拥有我的 POSTGRESQL 数据库中的所有功能。

通过数据库控制台当我运行 sql 查询上面的数据完美地显示在员工 table.

INSERT INTO company.employees ( employeeid,first_name, last_name, ssn, salary, title, birth_date, departmentid, address, city, postal_code) VALUES ( uuid_generate_v4(),'ANAA', 'testLAstName', '0123512343', 6000, 'Test', '1956-12-02', 1, 'Oakhill drive no 24', 'Arlington,VA', 34075);

这是我的 EmployeeDAL:

package awesomecompany.dal;

import awesomecompany.model.Employee;
import awesomecompany.utils.DBManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class EmployeeDAL extends AbstractDAL<Employee> {

    private PreparedStatement insertEmployeeStatement;
    private PreparedStatement updateEmployeeStatement;
    private static final String insertEmployeeSQL = "INSERT INTO company.employees VALUES (uuid_generate_v4(),?,?,?,?,?,?,?,?,?,?)";
    private static final String getAllEmployeeSQL = "SELECT * FROM company.employees";
    private static final String updateEmployeeSQL = "UPDATE company.employees" +
            " SET first_name = ?, last_name = ?, ssn = ?, salary = ?, title = ?, birth_date = ?, departmentid = ?, address = ?, city = ?, postal_code = ?" +
            " WHERE employeeid = ?;";
    private static final String deleteEmployeeSQL = "DELETE FROM company.employees WHERE employeeid =" ;

    @Override
    protected String deleteEntitySQL() {
        return deleteEmployeeSQL;
    }

    @Override
    protected String getAllEntitiesSQL() {
        return getAllEmployeeSQL;
    }


    @Override
    public Employee save(Employee entity){
        Connection connection = DBManager.getInstance().getConnection();
        try {
            insertEmployeeStatement = connection.prepareStatement(insertEmployeeSQL);
//            insertEmployeeStatement.setObject(1, entity.getId());
            insertEmployeeStatement.setString(1, entity.getFirstName());
            insertEmployeeStatement.setString(2, entity.getLastName());
            insertEmployeeStatement.setString(3, entity.getSsn());
            insertEmployeeStatement.setDouble(4, entity.getSalary());
            insertEmployeeStatement.setString(5,entity.getTitle());
            insertEmployeeStatement.setDate(6, Date.valueOf(entity.getBirthDate()));
            insertEmployeeStatement.setInt(7, entity.getDepartmentId());
            insertEmployeeStatement.setString(8, entity.getAddress());
            insertEmployeeStatement.setString(9, entity.getCity());
            insertEmployeeStatement.setInt(10, entity.getPostalCode());
            insertEmployeeStatement.executeUpdate();
            DBManager.getInstance().commitTransaction(connection);
        } catch (SQLException exception) {
            exception.printStackTrace();
            DBManager.getInstance().rollbackTransaction(connection);
        } finally {
            DBManager.getInstance().closeConnection(connection);
        }
        return entity;

    }

    @Override
    public Employee update(Employee entity) {
        Connection connection = DBManager.getInstance().getConnection();
        try{
            updateEmployeeStatement = connection.prepareStatement(updateEmployeeSQL);
            updateEmployeeStatement.setString(1, entity.getFirstName());
            updateEmployeeStatement.setString(2, entity.getLastName());
            updateEmployeeStatement.setString(3, entity.getSsn());
            updateEmployeeStatement.setDouble(4, entity.getSalary());
            updateEmployeeStatement.setString(5,entity.getTitle());
            updateEmployeeStatement.setDate(6, Date.valueOf(entity.getBirthDate()));
            updateEmployeeStatement.setInt(7, entity.getDepartmentId());
            updateEmployeeStatement.setString(8, entity.getAddress());
            updateEmployeeStatement.setString(9, entity.getCity());
            updateEmployeeStatement.setInt(10, entity.getPostalCode());
            updateEmployeeStatement.setObject(11, entity.getId());

            updateEmployeeStatement.executeUpdate();
            DBManager.getInstance().commitTransaction(connection);


        } catch (SQLException exception) {
            exception.printStackTrace();
            DBManager.getInstance().rollbackTransaction(connection);
        } finally {
            DBManager.getInstance().closeConnection(connection);
        }
        return entity;

    }



    @Override
    protected List<Employee> extractEntities(ResultSet employees) {
        List<Employee> employeeList = new ArrayList<>();
        try {
            while (employees.next()) {
                Employee employee = new Employee();
                employee.setId((UUID) employees.getObject("employeeid"));
                employee.setFirstName(employees.getString("first_name"));
                employee.setLastName(employees.getString("last_name"));
                employee.setSsn(employees.getString("ssn"));
                employee.setSalary(employees.getDouble("salary"));
                employee.setTitle(employees.getString("title"));
                employee.setBirthDate(employees.getDate("birth_date"));
                employee.setDepartmentId(employees.getInt("departmentid"));
                employee.setAddress(employees.getString("address"));
                employee.setCity(employees.getString("city"));
                employee.setPostalCode(employees.getInt("postal_code"));
                employeeList.add(employee);
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        return employeeList;
    }


}

这是我的员工 Object(我没有在此处包括 getters/setters,但我当然有):

public class 员工 {

private UUID id;
private String firstName;
private String lastName;
private String ssn;
private double salary;
private String title;
private LocalDate birthDate;
private int departmentId;
private String address;
private String city;
private int postalCode;

这是我 运行 在 Driver class 中给我错误的测试代码:

public class Driver {

public static void main(String[] args) {

    Employee employee = new Employee();
    employee.setFirstName("HELLO");
    employee.setLastName("WORLD");
    employee.setSsn("0123512343");
    employee.setSalary(6000.00);
    employee.setTitle("Test");
    employee.setBirthDate(LocalDate.of(1956,12,2));
    employee.setDepartmentId(1);
    employee.setAddress("Oakhill drive no 24");
    employee.setCity("Arlington,VA");
    employee.setPostalCode(34075);

    ServiceContext.getEmployeeService().save(employee);
}

}

这是我的员工 table 的样子:

-- auto-generated definition
create table employees
(
    employeeid   uuid        not null
        constraint employees_pkey
            primary key,
    first_name   varchar(50) not null,
    last_name    varchar(50) not null,
    ssn          varchar(50),
    salary       double precision,
    title        varchar(50),
    birth_date   date,
    departmentid integer
        constraint employees_departmentid_fk
            references departments,
    address      varchar(50),
    city         varchar(50),
    postal_code  integer
);

alter table employees
    owner to postgres;

您在不同的数据库中创建了扩展,或者您在不在 search_path 上的架构中创建了它。