package br.edu.ifba.inf008.contabil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TesteJDBC {
	
	public static void main(String[] args) throws SQLException {
		(new TesteJDBC()).run2();
	}
	
	private void run2() throws SQLException {
		System.out.println("Conectando...");
		DriverManager.registerDriver(new org.postgresql.Driver());
		Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "aluno");
		PreparedStatement stmt = conexao.prepareStatement("INSERT INTO CONTA(ID, NOME, SALDO, TIPO) VALUES(?, ?, ?, ?)");
		stmt.setString(1, "3.0");
		stmt.setString(2, "CAPITAL");
		stmt.setDouble(3, 0);
		stmt.setInt(4, 3);
		int rows = stmt.executeUpdate();
		System.out.println("Quantidade de linhas afetadas: " + rows);
		stmt.close();
		conexao.close();
		System.out.println("Finalizando...");
	}
	

	private void run1() throws SQLException {
		System.out.println("Conectando...");
		DriverManager.registerDriver(new org.postgresql.Driver());
		Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "aluno");
		Statement stmt = conexao.createStatement();
		ResultSet rSet = stmt.executeQuery("SELECT id, nome, saldo, tipo FROM CONTA");
		while(rSet.next()){
			String id = rSet.getString("id");
			String nome = rSet.getString("nome");
			double saldo = rSet.getDouble("saldo");
 			System.out.println(id + "--" + nome + "--" + saldo);
		}
		rSet.close();
		stmt.close();
		conexao.close();
		System.out.println("Finalizando...");
	}

}
