JAN
JAN

Reputation: 11

null value in column "empid" If relation "employee_infomation" violates not-null constraint

I've been working my java GUI on this for several days now, and I can't find a solution. I want to add an employee data and register it to my PostgreSQL database but something missing I have 1 database 2 schemas which 1 for login with a table for login_credential and 1 for employee_data with a table for employee_information. What should I do to fix this?

Do I need to recreate another table in my PostgreSQL?

My code in addEmployee.java:

private void jBtnAddActionPerformed(java.awt.event.ActionEvent evt) {

    try {
            String sql = "INSERT INTO employee_data.employee_information"
                    + "(first_name, last_name, birthday, address,"
                    + "phone_number, status, sss, philhealth, tin, pagibig,"
                    + "position, supervisor, rice_allowance, basic_salary, "
                    + "phone_allowance, clothing_allowance, gross_semi_monthly_rate, hourly_rate)VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    
  
            pst=conn.prepareStatement(sql);
            pst.setString(1,jTextFirstName.getText());
            pst.setString(2,jTextLastName.getText());
            pst.setString(3,jTextBirthday.getText());
            pst.setString(4,jTextAddress.getText());
            pst.setString(5,jTextPhoneNumb.getText());
            pst.setString(6,jTextStatus.getText());
            pst.setString(7,jTextSSS.getText());
            pst.setString(8,jTextPhilhealth.getText());
            pst.setString(9,jTextTin.getText());
            pst.setString(10,jTextPagibig.getText());
            pst.setString(11,jTextPosition.getText());
            pst.setString(12,jTextSupervisor.getText());
            pst.setString(13,jTextRice.getText());
            pst.setString(14,jTextPhone.getText());
            pst.setString(15,jTextClothing.getText());
            pst.setString(16,jTextBasicSalary.getText());
            pst.setString(17,jTextSemiMonthlyRate.getText());
            pst.setString(18,jTextHourlyRate.getText());
            
           
            

            pst.execute();
            JOptionPane.showMessageDialog(null,"Data is saved successfully");

        }catch (Exception e){
            
            JOptionPane.showMessageDialog(null,e);
      
        }finally {
            try{
                if (pst != null) {
                    pst.close();
                }

            }catch(Exception e){
               JOptionPane.showMessageDialog(null,e);
            }
         
        
     }

My code database:

CREATE TABLE IF NOT EXISTS employee_data.employee_information
(
empid numeric(4,0),
first_name character(25) COLLATE pg_catalog."default" NOT NULL,
last_name character(25) COLLATE pg_catalog."default" NOT NULL,
birthday character(20) COLLATE pg_catalog."default" NOT NULL,
address character(255) COLLATE pg_catalog."default" NOT NULL,
phone_number character(15) COLLATE pg_catalog."default" NOT NULL,
status character(20) COLLATE pg_catalog."default" NOT NULL,
sss character(20) COLLATE pg_catalog."default" NOT NULL,
philhealth character(20) COLLATE pg_catalog."default" NOT NULL,
tin character(20) COLLATE pg_catalog."default" NOT NULL,
pagibig character(20) COLLATE pg_catalog."default" NOT NULL,
"position" character(255) COLLATE pg_catalog."default" NOT NULL,
supervisor character(255) COLLATE pg_catalog. "default" NOT NULL,
rice_allowance character(10) COLLATE pg_catalog."default" NOT NULL,
basic_salary character(20) COLLATE pg_catalog."default" NOT NULL,
phone_allowance character(10) COLLATE pg_catalog."default" NOT NULL,
clothing_allowance character(10) COLLATE pg_catalog."default" NOT NULL,
gross_semi_monthly_rate character(20) COLLATE pg_catalog."default" NOT NULL,
hourly_rate character(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT employee_information_pkey PRIMARY KEY (empid)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS employee_data.employee_information
OWNER to admin;

Upvotes: 1

Views: 43

Answers (1)

Belayer
Belayer

Reputation: 14934

Your table definition includes the column empid and declares that column as primary key. This declaration imposes 2 requirements: it is unique and it is not null. However you do not define a default generation process not does your insert assign a value to it; therefore it is NULL and the source of your error. You should change the definition to:

CREATE TABLE IF NOT EXISTS employee_data.employee_information
( empid integer generated always as identity, 
  ... ,
  CONSTRAINT employee_information_pkey PRIMARY KEY (empid)
);

See documentation;

Upvotes: 0

Related Questions