Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.java
- package testabanco;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Connection c = new conecta().getConnection();
- int a = -1;
- while(a!= 0){
- System.out.println("##########Sistema de login###########");
- System.out.println("1 - cadastrar usuario");
- System.out.println("2 - logar");
- System.out.println("3 - atualizar");
- System.out.println("4 - deletar");
- Scanner in = new Scanner(System.in);
- a = in.nextInt();
- switch(a){
- case 1:
- //String sql = "insert into users values(NULL,\"jessica\",\"doida\",\"jessiquita\",\"senhatest\"";
- String sql = "insert into users values(NULL,?,?,?,?)";
- Scanner aff = new Scanner(System.in);
- System.out.print("Digite seu primeiro nome: ");
- String FirstName = aff.nextLine();
- System.out.print("Digite seu segundo nome: ");
- String LastName = aff.nextLine();
- System.out.print("Digite seu login: ");
- String username = aff.nextLine();
- System.out.print("Digite sua senha: ");
- String password = aff.nextLine();
- try{
- PreparedStatement stmt = c.prepareStatement(sql);
- stmt.setString(1, FirstName);
- stmt.setString(2, LastName);
- stmt.setString(3, username);
- stmt.setString(4, password);
- stmt.execute();
- System.out.println("Inserido com sucesso!!");
- stmt.close();
- }catch(SQLException e){
- throw new RuntimeException(e);
- }
- break;
- case 2:
- Scanner test = new Scanner(System.in);
- System.out.println();
- System.out.println("Entre com o usuario: ");
- String login = test.nextLine();
- System.out.println("Entre com a senha: ");
- String senha = test.nextLine();
- String sql2 = "select * from users where username = ? and password = ?";
- try{
- PreparedStatement stmt = c.prepareStatement(sql2);
- stmt.setString(1, login);// setstring pq e string ;d
- stmt.setString(2, senha);
- ResultSet teste = stmt.executeQuery();
- if(teste.next() != true){
- System.out.println("Usuario nao encontrado");
- System.exit(0);
- }
- System.out.println(teste.getString("username"));
- System.out.println(teste.getString("password"));
- if(!login.equals(teste.getString("username")) || !senha.equals(teste.getString("username"))){
- System.out.println("Usuario ou senha invalidos");
- System.exit(0);
- }
- //while(teste.next()){
- System.out.println("#############Voce esta logado############");
- System.out.println("ID: "+teste.getString("id"));
- System.out.println("Username: "+teste.getString("username"));
- System.out.println("Lastname: "+teste.getString("LastName"));
- //}
- stmt.close();
- }catch(SQLException e){
- throw new RuntimeException(e);
- }
- System.exit(0);
- break;
- case 3:
- String sql3 = "update users set FirstName = ? where id = 2";
- Scanner arriegua = new Scanner(System.in);
- System.out.println("Entre com o novo nome da jessica");
- String data = arriegua.nextLine();
- try{
- PreparedStatement stmt = c.prepareStatement(sql3);
- stmt.setString(1, data);// setstring pq e string ;d
- int teste = stmt.executeUpdate();
- stmt.close();
- }catch(SQLException e){
- throw new RuntimeException(e);
- }
- break;
- case 4:
- String sql4 = "delete from users where username = ?";
- Scanner arri = new Scanner(System.in);
- System.out.println("Entre com o nome a ser deletado");
- String bata = arri.nextLine();
- try{
- PreparedStatement stmt = c.prepareStatement(sql4);
- stmt.setString(1, bata);// setstring pq e string ;d
- boolean teste = stmt.execute();
- stmt.close();
- }catch(SQLException e){
- throw new RuntimeException(e);
- }
- }
- }
- }
- }
- <!--- conecta.java ---!>
- package testabanco;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class conecta {
- public conecta(){
- }
- public Connection getConnection(){
- System.out.println("Conectando ao banco de dados");
- try{
- return DriverManager.getConnection("jdbc:mysql://localhost/universidade","root","aluno");
- }catch(SQLException e){
- throw new RuntimeException(e);
- }
- }
- }
Add Comment
Please, Sign In to add comment