How do I make the student registration number of the logged in account stick in the published table?

Asked

Viewed 79 times

3

I am doing a rating project of a "college", I have the publication 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 do AR of the student logged in, who made the publication, go to the publication table that has a relation of 1:n with the student, that is, a student makes several publications.

  • 1

    When the user logs in do you store the data in session? If so, just take the RA that is in the session. If the AR is not, add this information when the student logs in.

  • student.Setra(Integer.parseint(txtRa.gettext()); student.setName(txtNome2.gettext()); student.setNome_user(txtLogin2.gettext()); student.setSenha(txtSenha2.gettext()); this would store the data in session?

  • No! In this case you’re just populating the object, is Vc using JSF? If it’s JSF it would be like this instantiating a Session SessionContext session = SessionContext.getInstance(); storing the data in a session = session.setAttribute("nomeUsuario", u.getNomeUsuario());- Retrieving an attribute from Session String nome = (String) session.getAttribute('nomeUsuario);.

  • No, the interface was done manually, not using framework :(

  • No problem. Search over Session and look for the implementation that fits your architecture.

1 answer

0

I don’t know if I understand your question correctly, but what you need to do is simply take this select out of 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 vc inserts the RA of the same with a Setra(ra).

Browser other questions tagged

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