0
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
// pode usar para alterar o nome da tabela la no banco
//@Table(name = "")
public class Usuario {
@Id
@GeneratedValue
private Long id;
@Column(name= "login_usu")
private String login;
private String senha;
public void mostrar() {
System.out.printf("Id: %-5d Login: %-30s Senha: %-20s \n", id, login, senha);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class ConnectionFactory {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("aula09");
public static EntityManager getEntityManager() {
return emf.createEntityManager();
}
}
import javax.persistence.EntityManager;
public class Principal {
public static void main(String[] args) {
Usuario u = new Usuario();
u.setLogin("[email protected]");
u.setSenha("senha123");
EntityManager em = ConnectionFactory.getEntityManager();
em.getTransaction().begin();
em.persist(u);
em.getTransaction();
em.close();
}
}
When ordered to run, everything is created but information is not inserted in the database.
Your password field has no annotation.
– Wilker
I edited your question because you are using JPA, not JDBC directly. Also, I took out the [sql] tag because you are not having trouble with
SELECT
s,INSERT
s,UPDATE
s andDELETE
s - you are not using them directly.– Victor Stafusa