Guest User

Untitled

a guest
Oct 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. package tarefaBD;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Scanner;
  9.  
  10. public class Teste {
  11.  
  12. public static void main(String[] args) throws Exception {
  13.  
  14. Pessoa inserirpessoa = new Pessoa();
  15. Scanner input = new Scanner(System.in);
  16.  
  17. try {
  18.  
  19. Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/Tarefa4.2", "root", "14725800");
  20.  
  21. System.out.println("Digite o nome: ");
  22.  
  23. String nome = input.nextLine();
  24.  
  25. inserirpessoa.setNome(nome);
  26.  
  27. System.out.println("Digite a idade: ");
  28. int idade = input.nextInt();
  29.  
  30. inserirpessoa.setIdade(idade);
  31.  
  32. System.out.println("Digite o endereço: ");
  33. String endereco = input.nextLine();
  34.  
  35. inserirpessoa.setEndereço(endereco);
  36.  
  37. PreparedStatement stmt = conexao.prepareStatement("INSERT INTO pessoa(nome, idade, endereço) VALUES ( ?, ?, ?)");
  38.  
  39. stmt.setString(1, nome);
  40.  
  41. stmt.setInt(2, idade);
  42.  
  43. stmt.setString(3, endereco);
  44.  
  45. stmt.execute();
  46.  
  47. ResultSet rs = stmt.executeQuery("Select * from pessoa");
  48.  
  49. System.out.println("Pessoa no Banco de Dados: ");
  50.  
  51. while (rs.next()) {
  52.  
  53. System.out.println(rs.getString(nome));
  54. System.out.println(rs.getInt(idade));
  55. System.out.println(rs.getString(endereco));
  56. }
  57.  
  58. System.out.println("Fim da leitura do banco de dados.");
  59.  
  60. conexao.close();
  61. rs.close();
  62. stmt.close();
  63. }
  64. catch (SQLException e) {
  65.  
  66. e.printStackTrace();
  67.  
  68. }
  69. }
  70.  
  71. }
  72.  
  73. Sat May 20 14:42:40 BRT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  74. Digite o nome:
  75. rogerio
  76. Digite a idade:
  77. 41
  78. Digite o endereço:
  79. Pessoa no Banco de Dados:
  80. java.sql.SQLException: Column 'rogerio' not found.
  81. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
  82. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
  83. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
  84. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
  85. at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1077)
  86. at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5192)
  87. at tarefaBD.Teste.main(Teste.java:53)
  88.  
  89. while (rs.next()) {
  90.  
  91. System.out.println(rs.getString("nome"));
  92. System.out.println(rs.getInt("idade"));
  93. System.out.println(rs.getString("endereco"));
  94. }
  95.  
  96. while (rs.next())
  97. {
  98. System.out.println(rs.getString("nome"));
  99. System.out.println(rs.getInt("idade"));
  100. System.out.println(rs.getString("endereco"));
  101. }
Add Comment
Please, Sign In to add comment