Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3. public class main {
  4. static final String url = "jdbc:mysql://127.0.0.1:3306/?user=root&password=davide2015&useSSL=false";
  5. static final String driver = "com.mysql.jdbc.Driver";
  6.  
  7. public static void main(String[] args) {
  8.  
  9. try
  10. {
  11. Scanner in = new Scanner(System.in);
  12. Class.forName(driver);
  13. Connection conn = DriverManager.getConnection(url);
  14.  
  15.  
  16. for(SQLWarning prb = conn.getWarnings(); prb != null; prb = prb.getNextWarning())
  17. {
  18. System.out.println("PROBLEMA SQL:");
  19. System.out.println("STATO : " + prb.getSQLState());
  20. System.out.println("MESSAGGIO: " + prb.getMessage());
  21. System.out.println("ERRORE : " + prb.getErrorCode());
  22. }
  23.  
  24. boolean done= false;
  25. int risp = 0;
  26. while(!done)
  27. {
  28. System.out.println("Quale Operazione eseguire?(Op.1 - Op.10)");
  29. String risposta = in.nextLine();
  30. risp = Integer.parseInt(risposta);
  31. if(risp >= 1 && risp <= 10)
  32. {
  33. done= true;
  34. }
  35. }
  36.  
  37. if(risp==1){
  38. System.out.println("Op.1 Inserire un'attività nel parco\n");
  39. System.out.println("Inserisci codice e nome dell'attività");
  40. String codice = in.nextLine();
  41. String nome = in.nextLine();
  42. Statement stato = conn.createStatement();
  43. PreparedStatement pr = conn.prepareStatement("INSERT INTO Attività values (?,?)");
  44. pr.setString(1, codice);
  45. pr.setString(2, nome);
  46. pr.execute();
  47. ResultSet rs = stato.executeQuery("SELECT * FROM Attività");
  48. System.out.println(" codice nome \n");
  49.  
  50. while (rs.next()){
  51. System.out.printf("%-8s",rs.getString(1));
  52. System.out.printf("%-13a",rs.getString(2));
  53.  
  54.  
  55. }
  56. rs.close();
  57. stato.close();
  58. pr.close();
  59.  
  60. }
  61.  
  62. }
  63. catch (Exception e)
  64. {
  65. System.out.println(e);
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement