Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Jdbc {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://sql10.freesqldatabase.com/Alunos";
- String user = "sql10163288";
- String psswrd = "DAvh4R4DkS";
- Connection con = DriverManager.getConnection(url, user, psswrd);
- String sql = "select nome from Alunos where matricula = ?";
- String sql2 = "update Alunos set nome = ?, idade = ?, turma = ? where matricula = ?";
- String sql3 = "insert into Alunos values ('?', ?, '?', '?')";
- String sql4 = "delete from Alunos where matricula = ?";
- PreparedStatement cons = con.prepareStatement(sql);
- PreparedStatement cons2 = con.prepareStatement(sql2);
- PreparedStatement cons3 = con.prepareStatement(sql3);
- PreparedStatement cons4 = con.prepareStatement(sql4);
- int opcao = 0;
- while (opcao != 0) {
- System.out.print("");
- System.out.print("1 - Buscas no Banco de Dados");
- System.out.print("2 - Alterar dados do Banco");
- System.out.print("3 - Cadastrar mais pessoas do Banco");
- System.out.print("4 - Deletar dados do Banco ");
- opcao = sc.nextInt();
- if (opcao == 1) {
- System.out.print("\n Busca no Banco de Dados");
- System.out.print("Digite a matrícula a ser pesquisada: ");
- String matricula = sc.next();
- cons.setString(1, matricula);
- ResultSet rs = cons.executeQuery();
- while (rs.next()) {
- System.out.print("Nome: " + rs.getString("nome"));
- }
- } else {
- if (opcao == 2) {
- System.out.print("\n Alterar dados do Banco");
- System.out.print("Digite a matrícula a ser alterada: ");
- String matricula = sc.next();
- System.out.print("\n Digite o nome: ");
- String nome = sc.next();
- System.out.print("Digite a idade: ");
- int idade = sc.nextInt();
- System.out.print("Digite a turma: ");
- String turma = sc.next();
- cons2.setString(1, nome);
- cons2.setInt(2, idade);
- cons2.setString(3, turma);
- cons2.setString(4, matricula);
- ResultSet rs = cons2.executeQuery();
- } else {
- if(opcao == 3){
- System.out.print("\n Cadastro de Alunos");
- System.out.print("Digite o nome");
- String nome = sc.next();
- System.out.print("Digite a idade");
- int idade = sc.nextInt();
- System.out.println("Digite a matrícula");
- String matricula = sc.next();
- System.out.print("Digite a turma");
- String turma = sc.next();
- cons3.setString(1, nome);
- cons3.setInt(2, idade);
- cons3.setString(3, matricula);
- cons3.setString(4, turma);
- ResultSet rs = cons3.executeQuery();
- } else {
- if(opcao == 4){
- System.out.print("\n CUIDADO AO DELETAR DADOS DO BANCO");
- System.out.print("Digite a matrícula");
- String matricula = sc.next();
- System.out.print("Digite novamente a matrícula");
- String matricula2 = sc.next();
- if (matricula == matricula2){
- cons4.setString(1, matricula);
- ResultSet rs = cons4.executeQuery();
- }
- }
- }
- }
- }
- }
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement