Guest User

Untitled

a guest
Mar 3rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. //main.java
  2. package testabanco;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Scanner;
  8.  
  9.  
  10. public class Main {
  11.     public static void main(String[] args) {
  12.         Connection c = new conecta().getConnection();
  13.        
  14.  
  15.         int a = -1;
  16.         while(a!= 0){
  17.         System.out.println("##########Sistema de login###########");
  18.         System.out.println("1 - cadastrar usuario");
  19.         System.out.println("2 - logar");
  20.         System.out.println("3 - atualizar");
  21.         System.out.println("4 - deletar");
  22.         Scanner in = new Scanner(System.in);
  23.         a = in.nextInt();
  24.         switch(a){
  25.             case 1:
  26.          //String sql = "insert into users values(NULL,\"jessica\",\"doida\",\"jessiquita\",\"senhatest\"";
  27.  
  28.                     String sql = "insert into users values(NULL,?,?,?,?)";
  29.                     Scanner aff = new Scanner(System.in);
  30.                     System.out.print("Digite seu primeiro nome: ");
  31.                     String FirstName = aff.nextLine();
  32.                     System.out.print("Digite seu segundo nome: ");
  33.                     String LastName = aff.nextLine();
  34.                     System.out.print("Digite seu login: ");
  35.                     String username = aff.nextLine();
  36.                     System.out.print("Digite sua senha: ");
  37.                     String password = aff.nextLine();
  38.                     try{
  39.                         PreparedStatement stmt = c.prepareStatement(sql);
  40.                         stmt.setString(1, FirstName);
  41.                         stmt.setString(2, LastName);
  42.                         stmt.setString(3, username);
  43.                         stmt.setString(4, password);
  44.  
  45.                         stmt.execute();
  46.                         System.out.println("Inserido com sucesso!!");
  47.                         stmt.close();
  48.                     }catch(SQLException e){
  49.                         throw new RuntimeException(e);
  50.                     }
  51.  
  52.         break;
  53.             case 2:
  54.                     Scanner test = new Scanner(System.in);
  55.                     System.out.println();
  56.                     System.out.println("Entre com o usuario: ");
  57.                     String login = test.nextLine();
  58.                     System.out.println("Entre com a senha: ");
  59.                     String senha = test.nextLine();
  60.                     String sql2 = "select * from users where username = ? and password = ?";
  61.  
  62.                     try{
  63.                         PreparedStatement stmt = c.prepareStatement(sql2);
  64.                         stmt.setString(1, login);// setstring pq e string ;d
  65.                         stmt.setString(2, senha);
  66.  
  67.  
  68.                         ResultSet teste = stmt.executeQuery();
  69.                         if(teste.next() != true){
  70.                             System.out.println("Usuario nao encontrado");
  71.                             System.exit(0);
  72.                         }
  73.                         System.out.println(teste.getString("username"));
  74.                         System.out.println(teste.getString("password"));
  75.                         if(!login.equals(teste.getString("username")) || !senha.equals(teste.getString("username"))){
  76.                             System.out.println("Usuario ou senha invalidos");
  77.                             System.exit(0);
  78.                         }
  79.                         //while(teste.next()){
  80.                         System.out.println("#############Voce esta logado############");
  81.                         System.out.println("ID: "+teste.getString("id"));
  82.                         System.out.println("Username: "+teste.getString("username"));
  83.                         System.out.println("Lastname: "+teste.getString("LastName"));
  84.                         //}
  85.                         stmt.close();
  86.                     }catch(SQLException e){
  87.                        
  88.                         throw new RuntimeException(e);
  89.                     }
  90.                    System.exit(0);
  91.                break;
  92.             case 3:
  93.                  String sql3 = "update users set FirstName = ? where id = 2";
  94.                  Scanner arriegua = new Scanner(System.in);
  95.                  System.out.println("Entre com o novo nome da jessica");
  96.                  String data = arriegua.nextLine();
  97.                     try{
  98.                         PreparedStatement stmt = c.prepareStatement(sql3);
  99.                         stmt.setString(1, data);// setstring pq e string ;d
  100.                         int teste = stmt.executeUpdate();
  101.                         stmt.close();
  102.                     }catch(SQLException e){
  103.                        
  104.                         throw new RuntimeException(e);
  105.                     }
  106.             break;
  107.             case 4:
  108.                  String sql4 = "delete from users where username = ?";
  109.                  Scanner arri = new Scanner(System.in);
  110.                  System.out.println("Entre com o nome a ser deletado");
  111.                  String bata = arri.nextLine();
  112.                     try{
  113.                         PreparedStatement stmt = c.prepareStatement(sql4);
  114.                         stmt.setString(1, bata);// setstring pq e string ;d
  115.                         boolean teste = stmt.execute();
  116.                         stmt.close();
  117.                     }catch(SQLException e){
  118.                        
  119.                         throw new RuntimeException(e);
  120.                     }
  121.          
  122.         }
  123.     }
  124.     }
  125. }
  126.  
  127. <!--- conecta.java ---!>
  128. package testabanco;
  129. import java.sql.Connection;
  130. import java.sql.DriverManager;
  131. import java.sql.SQLException;
  132. public class conecta {
  133.     public conecta(){
  134.     }
  135.     public Connection getConnection(){
  136.           System.out.println("Conectando ao banco de dados");
  137.           try{
  138.               return DriverManager.getConnection("jdbc:mysql://localhost/universidade","root","aluno");
  139.           }catch(SQLException e){
  140.               throw new RuntimeException(e);
  141.           }
  142.     }
  143.  
  144. }
Add Comment
Please, Sign In to add comment