java – How can I make the student's record number of the logged-in account appear in the published publication table?

Question:

I'm doing a "college" rating project, I have the publishing class and in it the method:

public String NovaPublicacao(Connection conn){

      String sqlInserir = "INSERT INTO Publicacao (Assunto, Conteudo, RA) VALUES (?, ?, (SELECT RA from Alunos where Nome = " + aluno.getNome() + "))";

      try (PreparedStatement stm = conn.prepareStatement(sqlInserir);) {
            stm.setString(1, getAssunto());
            stm.setString(2, getConteudo());
            stm.execute();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e1) {
                System.out.print(e1.getStackTrace());
            }
        }

      return getConteudo().toString();
    }

How to make RA of the logged in student, who made the publication, go to the publication table that has a 1:n relationship with the student, that is, a student makes several publications.

Answer:

I'm not sure I understood your question correctly, but what you need is to simply remove this select from your insert code and add one more line:

stm.setString(1, getAssunto());
stm.setString(2, getConteudo());
stm.setString(3, getRA());
stm.execute();

When the student logs in, you enter his RA with a setRA(ra).

Scroll to Top