Advertisement
moabcavsaenjoy

BIBBIA

Oct 2nd, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. public class ConnessioneDatabase {
  6.  
  7.  
  8. private static Connection conn = null;
  9.  
  10. public static synchronized Connection getConnessione() throws ClassNotFoundException {
  11.  
  12. try {
  13. Class.forName("com.mysql.cj.jdbc.Driver");
  14. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/provadatabase","root","");
  15. } catch (SQLException e) {
  16. throw new RuntimeException(e);
  17. }
  18. return conn;
  19.  
  20.  
  21. }
  22. }
  23.  
  24.  
  25. import java.sql.Connection;
  26. import java.sql.PreparedStatement;
  27. import java.sql.SQLException;
  28.  
  29.  
  30.  
  31.  
  32.  
  33. public class Inserimento {
  34.  
  35. public int inserisciPersona (Utente p) throws ClassNotFoundException {
  36.  
  37. String sql = "INSERT INTO utenti (Nome, COGNOME, Eta, Genere) VALUES (?, ?, ?, ?)";
  38. Connection conn = ConnessioneDatabase.getConnessione();
  39. int resultSet=0;
  40. try
  41. {
  42. PreparedStatement ps = conn.prepareStatement(sql);
  43. ps.setString(1,p.getNome());
  44. ps.setString(2, p.getCognome());
  45. ps.setInt(3, p.getEta());
  46. ps.setString(4, p.getGenere());
  47. resultSet = ps.executeUpdate();
  48. ps.close();
  49.  
  50. System.out.println("Persona aggiunta");
  51. }
  52. catch (SQLException e)
  53. {
  54. System.out.println("Eccezione SQL");
  55. }
  56. return resultSet;
  57.  
  58.  
  59.  
  60.  
  61. Inserimento c = new Inserimento();
  62.  
  63. Utente p = new Utente();
  64.  
  65. response.setContentType("text/html;charset=UTF-8");
  66. try (PrintWriter out = response.getWriter()) {
  67. out.println("<h1> Inserimento dati! </h1>");
  68.  
  69. String nome = request.getParameter("nome");
  70. p.setNome(nome);
  71. String cognome = request.getParameter("cognome");
  72. p.setCognome(cognome);
  73. int eta = Integer.parseInt(request.getParameter("eta"));
  74. p.setEta(eta);
  75. String Genere = request.getParameter("genere");
  76. p.setGenere(Genere);
  77. try {
  78. c.inserisciPersona(p);
  79. } catch (ClassNotFoundException e) {
  80. System.out.println("ERRORE");
  81. }
  82.  
  83.  
  84.  
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement