Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Classe aluno:
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package projetobd;
- /**
- *
- * @author aluno
- */
- public class Aluno {
- private String nome;
- private int id;
- private String cpf;
- private int idade;
- public Aluno (String nome, String cpf, int idade){
- this.nome = nome;
- this.cpf = cpf;
- this.idade = idade;
- }
- public void setNome (String nome){
- this.nome = nome;
- }
- public String getNome (){
- return this.nome;
- }
- public void setId (int id){
- this.id = id;
- }
- public int getId (){
- return this.id;
- }
- public void setCPF (String cpf){
- this.cpf = cpf;
- }
- public String getCPF (){
- return this.cpf;
- }
- public void setIdade (int idade){
- this.idade = idade;
- }
- public int getIdade (){
- return this.idade;
- }
- }
- --
- Classe banco:
- package projetobd;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author aluno
- */
- public class Banco {
- public Connection conexao=null;
- public Statement declaracao=null;
- public ResultSet resultado=null;
- public void conectar(){
- String usuario = "root";
- String senha = "";
- String driver = "com.mysql.jdbc.Driver";
- String servidor = "jdbc:mysql://localhost:3306/bancopi";
- try{
- this.conexao = DriverManager.getConnection(servidor, usuario, senha);
- this.declaracao = this.conexao.createStatement();
- }catch(Exception e){
- System.out.println("Erro: " +e.getMessage());
- }
- }
- public boolean estaConectado(){
- if (conexao!= null){
- return true;
- } else
- return false;
- }
- public void insereAluno (Aluno aluno){
- try {
- String sql = "INSERT INTO ALUNO (NOME, CPF, IDADE) VALUES ('"+aluno.getNome()+"','"+aluno.getCPF()+"',"+aluno.getIdade()+")";
- this.declaracao.executeUpdate(sql);
- }catch(Exception e){
- System.out.println("Erro: "+e.getMessage());
- }
- }
- public ArrayList<Aluno> listarAlunos(){
- ArrayList<Aluno> alunos = new ArrayList<>();
- try {
- String sql = "SELECT * FROM ALUNO";
- Aluno a1;
- this.resultado = this.declaracao.executeQuery(sql);
- while (this.resultado.next()){
- String nome, cpf;
- int idade, id;
- }
- }catch(Exception e){
- System.out.println("Erro: "+e.getMessage());
- }
- return null;
- }
- }
- -- Classe main
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package projetobd;
- /**
- *
- * @author aluno
- */
- public class ProjetoBD {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Banco bd = new Banco();
- bd.conectar();
- boolean result = bd.estaConectado();
- if (result){
- System.out.println("Está conectado! xP\n");
- } else
- System.out.println("Não conectou ;_;");
- Aluno a1 = new Aluno("Leticia", "886767", 10);
- String sql = "INSERT INTO ALUNO (NOME, CPF, IDADE) VALUES ('"+a1.getNome()+"','"+a1.getCPF()+"',"+a1.getIdade()+")";
- System.out.println(sql);
- bd.insereAluno(a1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement