Guest User

Untitled

a guest
Feb 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. public class ConnectionFactory {
  2.  
  3. public static Connection getConnectionFactory() throws SQLException, ClassNotFoundException{
  4.  
  5. String url = "jdbc:postgresql://localhost:5432/topografiaJava";
  6. String usuario = "postgres";
  7. String senha = "Maker@1";
  8. Class.forName("org.postgresql.Driver");
  9. Connection con;
  10. con = DriverManager.getConnection(url, usuario, senha);
  11. System.out.println("Conexão realizada com sucesso.");
  12. Statement st1;
  13. st1 = con.createStatement();
  14. return con;
  15.  
  16. }
  17. }
  18.  
  19. public class PessoaDAO {
  20.  
  21. private Connection conn;
  22.  
  23. private Pessoa resultSet2Model(ResultSet rs) throws SQLException, ClassNotFoundException {
  24.  
  25. Pessoa p;
  26. p = new Pessoa(rs.getInt("id"),
  27. rs.getString("nome"),
  28. rs.getString("matricula"),
  29. rs.getString("setor"),
  30. rs.getString("login"),
  31. rs.getString("senha"),
  32. rs.getString("email"));
  33. return p;
  34. }
  35.  
  36. public Pessoa inserir(Pessoa p) throws SQLException, ClassNotFoundException {
  37. PreparedStatement st1=null;
  38. this.conn = new ConnectionFactory().getConnectionFactory();
  39. st1 = conn.prepareStatement("insert into pessoa (id, nome, matricula, setor, login, senha, email) values(?,?,?,?,?,?,?)");
  40. st1.setInt(1,p.getID);
  41. st1.setString(2, p.getNome());
  42. st1.setString (3, p.getMatricula());
  43. st1.setString(4, p.getSetor());
  44. st1.setString(5, p.getLogin());
  45. st1.setString(6,p.getSenha());
  46. st1.setString(7, p.getEmail());
  47. ResultSet rs1 = st1.executeQuery();
  48. rs1.next();
  49. st1.execute();
  50. return p;
  51. }
  52. }
Add Comment
Please, Sign In to add comment