Netbeans, java and database. Problems when inserting

1

Hello, I have this part of the code:

public void inserir(Assignatura as) throws GestorException  {
        //TODO codificar el metode inserir

    String sentenciaSQL = null;
    PreparedStatement elm = null;

    try{         

        elm = con.prepareStatement(sentenciaSQL);

 // elm = con.prepareStatement("INSERT INTO assignatura (codi, assignatura, dies");

  sentenciaSQL = "INSERT INTO assignatura VALUES (1, "
          +"'Mates','dilluns')";
  elm.executeUpdate(sentenciaSQL);


  //elm.setObject(1,as);
  //elm.executeUpdate();


    } catch (SQLException ex){
        System.out.println("error!"+ex.getMessage());
        System.out.println("SQLsState:"+ ex.getSQLState());
    }finally{
        try {
if(elm!=null && !elm.isClosed()){
elm.close();
}
} catch (SQLException ex) {/*llàstima!*/}
try {
if(con!=null && !con.isClosed()){
con.close();
}
      } catch (SQLException ex) {

            Logger.getLogger(GestorAssignatura.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

I'm trying things out but I do not know 100% how to do it .. the sql is this:

CREATE TYPE dades_assignatura as (nom character varying(30), hores integer);


CREATE TABLE Assignatura
(
  codi integer NOT NULL,
  assignatura dades_assignatura,
  dies character varying(9)[],
  CONSTRAINT clauPrimariaAssignatura PRIMARY KEY (codi)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE Assignatura
  OWNER TO postgres;

I'm going crazy ... I do not know 100% how it's done and I do not find information either: (

    
asked by Montse Mkd 06.03.2018 в 18:09
source

1 answer

2

I see that you insert in the value 3 data one of an integer type and two of type text, but your database has fields of a different type than the insert that you indicate.

This is an example for the insert of your table INSERT INTO public.assignatura(codi, assignatura, dies)VALUES (1, '("lulu",1)', '{"lunes"}'); but I do not know if your last field should be an arrangement dies character varying(9)[], or alone dies character varying(9),

    
answered by 06.03.2018 / 18:31
source