Insert error in Oracle database with Java

Asked

Viewed 77 times

0

When I make a set of insertions in the bank, it says that only the information of the course were inserted, but gang nay.

Console

STATUS DE CURSO: true 
STATUS DE CURSO: false

Joptionpane

inserir a descrição da imagem aqui

Touradao

public class TurmaDAO extends DAO {

 public TurmaDAO() {
        connection = Conexao.getConnection();
    }

    public boolean inserir(Turma turma, Curso curso) {
        boolean status = false;
        sql = "INSERT INTO turmas VALUES (seq_id_turma.nexval, ?, ?, ?)";
        try {
            p = connection.prepareStatement(sql);
            p.setString(1, turma.getNome());
            p.setString(2, curso.getNome());
            p.setString(3, curso.getPeriodo());
            p.execute();
            status = true;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível armazenar os dados da turma\n" + ex);
        }
        return status;
    }

Gang

public class Turma {
    private int id;
    private String nome;

    public Turma(String nome) {
        this.nome = nome;
    }

    public int getId() {
        return id;
    }

    public String getNome() {
        return nome;
    }

}

Testaturmadao

public class TestaTurmaDAO {

    public static void main(String[] args) {
        inserir();
    }

    public static void inserir() {
        Curso cs = new Curso("Sistemas de Informação", "Tarde");
        CursoDAO csDao = new CursoDAO();
        boolean csStatus = csDao.inserir(cs);
        System.out.println("STATUS DE CURSO: " + csStatus);

        Turma tr = new Turma("2SIA");
        TurmaDAO trDao = new TurmaDAO();
        boolean trStatus = trDao.inserir(tr, cs);
        System.out.println("STATUS DE TURMA: " + trStatus);
    }

Bank structure

Nome          Nulo?    Tipo          
------------- -------- ------------- 
ID_TURMA      NOT NULL NUMBER(5)     
NOME          NOT NULL VARCHAR2(100) 
NOME_CURSO    NOT NULL VARCHAR2(100) 
PERIODO_CURSO NOT NULL VARCHAR2(100) 
  • 1

    Instead of returning a dialog box, return the complete stack of errors, so that the error and its origin can be seen better. And add that stack to the question.

  • Forgive me for ignorance, but what would add to the stack of errors? Are those errors that appear when the application breaks?

  • 1

    Comment the optionpane line and add ex.printStackTrace();. When you pop the error, add the question to the stack.

  • I added the error stack information.

  • Where is line 19 of the Cursodao class?

  • Oops! I found the error! The syntax of Insert is with seq_id_turma.nexval, missed the’T' to be seq_id_turma.nexTval, but now you’re making another mistake.

Show 1 more comment

1 answer

1


The error message described in JOptionPane says a syntax error is happening.

When looking at variable syntax sql in the method inserir(), I noticed that a "T" is missing in the function .nextval of seq_id_turma oracle SQL.

Fix the syntax by adding a "T" and it will work again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.