Advertisement
Guest User

Untitled

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