Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3. public class Driver {
  4.  
  5.     public static void main(String[] args) throws SQLException {
  6.         Connection myConn = null;
  7.         Statement myStmt = null;
  8.         ResultSet myRs = null;
  9.         Scanner in = new Scanner(System.in);
  10.         System.out.println("Inserisci nome: ");
  11.         String a = in.nextLine();
  12.         System.out.println("Inserisci cognome : ");
  13.         String b = in.nextLine();
  14.        
  15.         try {
  16.             // 1. Creo una connessione al Database
  17.             myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/prova?useSSL=false", "root" , "home");
  18.            
  19.             // 2. Creo uno Statement
  20.             myStmt = myConn.createStatement();
  21.            
  22.             // 3. Eseguo le Query SQL
  23.             myRs = myStmt.executeQuery("SELECT * FROM studenti WHERE Nome = "+ a +" AND Cognome = "+ b +" ");
  24.            
  25.             // 4. Elaboro il risultato
  26.             while (myRs.next()) {
  27.                 System.out.println(myRs.getString("Nome") + ", " + myRs.getString("Cognome"));
  28.             }
  29.         }
  30.         catch (Exception exc) {
  31.             exc.printStackTrace();
  32.         }
  33.         finally {
  34.             if (myRs != null) {
  35.                 myRs.close();
  36.             }
  37.            
  38.             if (myStmt != null) {
  39.                 myStmt.close();
  40.             }
  41.            
  42.             if (myConn != null) {
  43.                 myConn.close();
  44.             }
  45.             in.close();
  46.         }
  47.  
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement