Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package model;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import org.hibernate.Session;
- /**
- *
- * @author Henri
- */
- public class PessoaDAO {
- String endereco = "jdbc:mysql://localhost:3306/aulapoo";
- String comando = "insert into pessoa(codigo,nome) values(?,?)";
- Connection conn = null;
- public static void salvar(Pessoa p)
- {
- Session session = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- session.save(p);
- session.getTransaction().commit();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- finally {
- if(session != null)
- session.close();
- }
- }
- public static List<Pessoa> listar()
- {
- ArrayList<Pessoa> pessoas = new ArrayList<Pessoa>();
- String sql = "select * from pessoa";
- Connection conn = null;
- try{
- String url ="jdbc:mysql://localhost:3306/aulapoo";
- conn = DriverManager.getConnection(url,"root","");
- PreparedStatement pst = conn.prepareStatement(sql);
- ResultSet rs = pst.executeQuery();
- while(rs.next())
- {
- Pessoa p = new Pessoa();
- p.setCodigo(rs.getInt("codigo"));
- p.setNome(rs.getString("nome"));
- pessoas.add(p);
- }
- }
- catch (SQLException e){
- throw new RuntimeException(e);
- }
- finally{
- try{
- if(conn != null && !conn.isClosed())
- conn.close();
- } catch (SQLException e){
- e.printStackTrace();
- }
- }
- return pessoas;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement