1
I have an application made in Java, and I need to store the user’s registration information in the database:
Cpf, password, name, address, telephone
System.out.println("Digite seu CPF");
cpf = ler.nextLine();
System.out.println("Senha: ");
senha = ler.nextLine();
System.out.print("Nome Completo: ");
nome = ler.nextLine();
System.out.print("Endereço: ");
endereco = ler.nextLine();
System.out.print("Telefone: ");
telefone = ler.nextInt();
I want to get this data through the Scanner class and save to the database.
To manage the database I am using Postgressql, and the IDE for the application is Eclipse.
I’ve already connected the database to Eclipse through JDBC, but I don’t know how to get user inputs and store them in the database using the Java Scanner Class. I searched the internet and the only tutorials I find is using Jframe and the IDE is Netbeans.
//Conexão do eclipse com o postgresql
import java.sql.*;
public class ConexaoSQL {
//Statement = Realizar pesquisa no banco de dados
public Statement stm;
//ResulSet = Armazenar o resultado da pesquisa
public ResultSet rs;
//Driver = identifica no serviço do banco de dados
private String driver = "org.postgresql.Driver";;
//Caminho = Indica qual o diretório do banco de dados
private String caminho = "jdbc:postgresql://localhost:5432/agencia";;
/*
Usuário: postgres
Senha: admin
*/
private String usuario = "postgres";;
private String senha = "admin";;
//Conection = Realizar a Conexão com banco de Dados
public Connection con;
public void conexao(){ //conecta ao banco de dados
System.setProperty("jdbc.Drivers", driver);
try {
con=DriverManager.getConnection(caminho, usuario, senha);
System.out.println("Conectado com Sucesso ao SQL!");
} catch (SQLException e) {
System.err.println("Erro de conexão ao SQL");
e.printStackTrace();
}
}
public void desconecta(){//desconecta do banco de dados
try {
con.close();
System.out.println("Desconectado com Sucesso!");
} catch (SQLException e) {
System.err.println("Erro ao fechar conexão com banco de dados");
e.printStackTrace();
}
}
}
Could someone help me or send me a tutorial that has something similar? Thanks for any help.
Table PostGreSQL
What have you done? Add the code of your form and your connection class to the question.
– user28595
I added what I did at the time.
– Alexandre Vieira de Souza
Alexandre your application is only in text mode?
– user28595
Yes, text only, all information is printed on the console.
– Alexandre Vieira de Souza
Young, it’s the same thing using Jframe or Scanner. Only changes where the data comes from.
– Jéf Bueno
Which columns have your table that want to save the data and which column type?
– user28595
Table: Client Colum: Cpf password name address phone
– Alexandre Vieira de Souza
@Alexandrevieiradesouza if all the information we have to keep asking separately or several times, it will be difficult to help you. All the questions I asked were very clear, but I have to ask you again to answer them. Read what was asked and try to answer as fully as possible.
– user28595
@diegofm I’m sorry for the inconvenience, I’m new on the forum, I’m still learning how to use some of the features. I inserted an image of the table with the columns in the question, all the columns were created are of type Character Varying. The java application is only in text mode and all information will be printed in the console.
– Alexandre Vieira de Souza