Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- public class Query {
- private Scanner t, in, k;
- //Attributi privati
- private String url, username, password;
- //Metodo Costruttore
- public Query(){
- this.url = "jdbc:mysql://localhost:3306/prova?useSSL=false";
- this.username = "root";
- this.password = "home";
- }
- public void Selezione(){
- try {
- // 1. Creo la connessione al DB
- Connection myConn = DriverManager.getConnection(url,username,password);
- // 2.Creo lo Statement
- Statement myStmt = myConn.createStatement();
- k = new Scanner(System.in);
- System.out.println("/////////////////////////////////////////////////////////////");
- System.out.println("Premi 1 per visualizzare tutti i dati delle tabelle");
- System.out.println("Premi 2 per ricercare dati in base alle tue esigenze");
- System.out.println("/////////////////////////////////////////////////////////////");
- int j = k.nextInt();
- System.out.println();
- System.out.println("Database prova -> Tabella studenti :");
- System.out.println();
- switch (j) {
- case 1:
- // 3. Creo la query da eseguire
- ResultSet myRs = myStmt.executeQuery("SELECT * FROM studenti");
- // 4. Elaboro il risultato
- System.out.println("| idStudenti | Nome | Cognome | Età |");
- System.out.println("-------------------------------------------------------");
- while (myRs.next()) {
- System.out.println("\t" + myRs.getInt("idStudenti") + "\t" + myRs.getString("Nome")
- + " \t" + myRs.getString("Cognome") + " \t" + myRs.getInt("Età"));
- }
- break;
- case 2 :
- t = new Scanner(System.in);
- System.out.println("Inserire idStudenti");
- int y = t.nextInt();
- // 3. Eseguo le Query SQL
- /**
- * Questa query permette di visualizzare se gli elementi inseriti da tastiera sono presenti nel DB
- */
- myRs = myStmt.executeQuery("SELECT * FROM studenti WHERE idStudenti = '"+ y +"'");
- System.out.println();
- System.out.println("| Nome | Cognome | Età |");
- System.out.println("-------------------------------------------------------");
- // 4. Elaboro il risultato
- while (myRs.next()) {
- System.out.println("\t" + myRs.getString("Nome") + " \t " + myRs.getString("Cognome") + " \t " + myRs.getString("Età"));
- }
- break;
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println();
- System.out.println("Lettura Database completata!");
- System.out.println();
- }
- public void Inserimento(){
- in = new Scanner(System.in);
- t = new Scanner(System.in);
- System.out.println("Inserisci idStudenti : ");
- int n = in.nextInt();
- if((n == 0)|| (n >= 1)){
- System.out.println("L'idStudente deve essere un numero diverso da 0 e maggiore di 1");
- System.out.println();
- System.out.println("Inserimento annullato e programma terminato!");
- System.exit(0);
- }
- System.out.println("Inserisci Nome : ");
- String name = t.nextLine();
- System.out.println("Inserisci Cognome : ");
- String surname = t.nextLine();
- System.out.println("Inserire Età : ");
- int age = in.nextInt();
- try {
- // 1. Creo una connessione al Database
- Connection myConn = DriverManager.getConnection(url,username,password);
- // 2. Creo uno Statement
- Statement myStmt = myConn.createStatement();
- // 3. Eseguo le Query SQL
- /**
- * Questa query permette inserire nuovi dati nel DB
- */
- String sql = "INSERT INTO studenti " + "(idStudenti,Nome,Cognome,Età)" + "VALUES ('"+n+"','"+name+"','"+surname+"','"+age+"')";
- myStmt.executeUpdate(sql);
- System.out.println("Inserimento Completato!");
- }catch (Exception exc) {
- //Il metodo printStackTrace() restituisce la sequenza di metodi invocati fino al punto in cui è avvenuta l'eccezione
- exc.printStackTrace();
- }
- }
- public void Elimina(){
- String url = "jdbc:mysql://localhost:3306/prova?useSSL=false";
- String user = "root";
- String password = "home";
- in = new Scanner(System.in);
- System.out.println("Inserire l'idStudente in base al quale deve essere eliminata la riga : ");
- int key = in.nextInt();
- if(key == 0){
- System.out.println("Non puoi eliminare la riga con idStudente uguale a 0!");
- }
- try {
- // 1. Creo la connessione al DB
- Connection myConn = DriverManager.getConnection(url,user, password);
- // 2. Creo lo Statement
- Statement myStmt = myConn.createStatement();
- // 3. Eseguo le Query SQL
- String sql = "DELETE FROM studenti WHERE idStudenti = '" + key + "' ";
- int riga = myStmt.executeUpdate(sql);
- System.out.println();
- System.out.println("N° di righe cancellate : " + riga);
- System.out.println();
- System.out.println("Eliminazione completata");
- System.out.println();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement