Inclusion of data by the operator

Asked

Viewed 85 times

2

I built the code below but when I insert the data the system shows 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 shown in 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)

2 answers

1

You are passing as 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 method getString class ResultSet expects to receive the column name like String or the column input as whole. In your code, you were passing the contents of local variables nome, idade and endereco, newly captured via text input.

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

0

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

Change the code block inside the while for

while (rs.next()) 
{
    System.out.println(rs.getString("nome"));
    System.out.println(rs.getInt("idade"));
    System.out.println(rs.getString("endereco"));
}
  • Thanks for the help. It worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.