Guest User

Untitled

a guest
Nov 15th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. public class TestaLista {
  2. public static void main(String[] args) throws RuntimeException{
  3.  
  4. ContatoDao dao = new ContatoDao();
  5. List<Contato> contatos = dao.getLista(); // <-- Aponta erro nessa parte
  6.  
  7. for(Contato contato : contatos) {
  8. System.out.println("Nome: " + contato.getNome());
  9. System.out.println("Email: " + contato.getEmail());
  10. System.out.println("Endereço: " + contato.getEndereco());
  11. System.out.println("Data de Nascimento: " +
  12. contato.getDataNascimento().getTime() + "n") ;
  13. }
  14. }
  15. }
  16.  
  17. public class ContatoDao {
  18. private Connection connection;
  19.  
  20. public void adiciona(Contato contato) {
  21. String sql = "insert into contatos" +
  22. "(nome, email, endereco, dataNascimento)" +
  23. "values(?,?,?,?)";
  24. }
  25.  
  26. public List<Contato> getLista(){
  27. try {
  28. List<Contato> contatos = new ArrayList<Contato>();
  29. PreparedStatement stmt = this.connection
  30. .prepareStatement("select * from contatos"); // <-- Aponta erro nessa parte...
  31. ResultSet rs = stmt.executeQuery();
  32.  
  33. while(rs.next()) {
  34.  
  35. //Objeto contato
  36. Contato contato = new Contato();
  37. contato.setId(rs.getLong("id"));
  38. contato.setNome(rs.getString("nome"));
  39. contato.setEmail(rs.getString("email"));
  40. contato.setEndereco(rs.getString("endereco"));
  41.  
  42. //montando a data com Calendar
  43. Calendar data = Calendar.getInstance();
  44. data.setTime(rs.getDate("dataNascimento"));
  45. contato.setDataNascimento(data);
  46.  
  47. contatos.add(contato);
  48. }
  49. rs.close();
  50. stmt.close();
  51. return contatos;
  52. }catch(SQLException e) {
  53. throw new RuntimeException(e);
  54. }
  55. }
  56. }
  57.  
  58. public class ConnectionFactory {
  59. public Connection getConnection() {
  60. try {
  61. return DriverManager.getConnection(
  62. "jdbc:mysql://localhost/fj21?useSSL=false", "root", "");
  63. } catch (SQLException e) {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67. }
  68.  
  69. public class ConnectionTeste {
  70. public static void main(String[] args) throws SQLException {
  71.  
  72. Connection connection = new ConnectionFactory().getConnection();
  73. System.out.println("Conexão aberta!");
  74. connection.close();
  75. }
  76. }
  77.  
  78. public class ConnectionFactory {
  79. public static Connection getConnection() {
  80. try {
  81. return DriverManager.getConnection(
  82. "jdbc:mysql://localhost/fj21?useSSL=false", "root", "");
  83. } catch (SQLException e) {
  84. throw new RuntimeException(e);
  85. }
  86. }
  87. }
  88.  
  89. public class ConnectionTeste {
  90. public static void main(String[] args) {
  91. try (Connection connection = ConnectionFactory.getConnection()) {
  92. System.out.println("Conexão aberta!");
  93. }
  94. }
  95. }
  96.  
  97. public class ContatoDao {
  98.  
  99. private static final String INSERT_SQL = ""
  100. + "INSERT INTO Contatos"
  101. + "(nome, email, endereco, dataNascimento)"
  102. + "VALUES (?, ?, ?, ?)";
  103.  
  104. private static final String SELECT_SQL = "SELECT * FROM Contatos";
  105.  
  106. public void adiciona(Contato contato) {
  107. // String sql = INSERT_SQL;
  108. }
  109.  
  110. public List<Contato> getLista() {
  111. try (
  112. Connection c = ConnectionFactory.getConnection();
  113. PreparedStatement stmt = c.prepareStatement(SELECT_SQL);
  114. ResultSet rs = stmt.executeQuery();
  115. ) {
  116. List<Contato> contatos = new ArrayList<>();
  117. while (rs.next()) {
  118.  
  119. //Objeto contato
  120. Contato contato = new Contato();
  121. contato.setId(rs.getLong("id"));
  122. contato.setNome(rs.getString("nome"));
  123. contato.setEmail(rs.getString("email"));
  124. contato.setEndereco(rs.getString("endereco"));
  125.  
  126. //montando a data com Calendar
  127. Calendar data = Calendar.getInstance();
  128. data.setTime(rs.getDate("dataNascimento"));
  129. contato.setDataNascimento(data);
  130.  
  131. contatos.add(contato);
  132. }
  133. return contatos;
  134. } catch(SQLException e) {
  135. throw new RuntimeException(e);
  136. }
  137. }
  138. }
  139.  
  140. public class TestaLista {
  141.  
  142. public static void main(String[] args) {
  143. ContatoDao dao = new ContatoDao();
  144. List<Contato> contatos = dao.getLista();
  145.  
  146. for (Contato contato : contatos) {
  147. System.out.println("Nome: " + contato.getNome());
  148. System.out.println("Email: " + contato.getEmail());
  149. System.out.println("Endereço: " + contato.getEndereco());
  150. System.out.println("Data de Nascimento: " +
  151. contato.getDataNascimento().getTime() + "n") ;
  152. }
  153. }
  154. }
Add Comment
Please, Sign In to add comment