Inclusion of data by the operator

2

I build the code below but when I enter the data the system has an error.

package tarefaBD;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Teste {

    public static void main(String[] args) throws Exception {

        Pessoa inserirpessoa = new Pessoa();
        Scanner input = new Scanner(System.in);

        try {

        Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/Tarefa4.2", "root", "14725800");

         System.out.println("Digite o nome: ");

         String nome = input.nextLine();

         inserirpessoa.setNome(nome);

         System.out.println("Digite a idade: ");
         int idade = input.nextInt();

         inserirpessoa.setIdade(idade);

         System.out.println("Digite o endereço: ");
         String endereco = input.nextLine();

         inserirpessoa.setEndereço(endereco);

         PreparedStatement stmt = conexao.prepareStatement("INSERT INTO pessoa(nome, idade, endereço) VALUES ( ?, ?, ?)");  

         stmt.setString(1, nome);

         stmt.setInt(2, idade);

         stmt.setString(3, endereco);

         stmt.execute();

         ResultSet rs = stmt.executeQuery("Select * from pessoa");

         System.out.println("Pessoa no Banco de Dados: ");

         while (rs.next()) {

         System.out.println(rs.getString(nome));
         System.out.println(rs.getInt(idade));
         System.out.println(rs.getString(endereco));
         }

         System.out.println("Fim da leitura do banco de dados.");

              conexao.close();
              rs.close();
              stmt.close();
        }
        catch (SQLException e) {

            e.printStackTrace();

        }
    }

}

Error displayed on console:

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.
Digite o nome: 
rogerio
Digite a idade: 
41
Digite o endereço: 
Pessoa no Banco de Dados: 
java.sql.SQLException: Column 'rogerio' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1077)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5192)
    at tarefaBD.Teste.main(Teste.java:53)
    
asked by anonymous 20.05.2017 / 21:42

2 answers

1

You are passing in the column names the captured text entries, when you should pass the column names as String , as below:

 while (rs.next()) {

     System.out.println(rs.getString("nome"));
     System.out.println(rs.getInt("idade"));
     System.out.println(rs.getString("endereco"));
 }

The getString method of the ResultSet class expects to receive the column name as String or the column index as integer. In your code, you were passing the content of the local variables nome , idade and endereco , newly captured via text entry.

One tip is to separate application logic and model logic to make your code more organized and easier to maintain.

    
20.05.2017 / 21:47
0

The method getString() from ResultSet gets the name (if it is string ) or index (if it is a int ) of a column as a parameter, you are passing the data that was entered by the operator.

Change the code block within while to

while (rs.next()) 
{
    System.out.println(rs.getString("nome"));
    System.out.println(rs.getInt("idade"));
    System.out.println(rs.getString("endereco"));
}
    
20.05.2017 / 21:48