Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package modello;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class GuestDataBean {
  12.  
  13. private Connection c;
  14. private Statement s;
  15.  
  16. // costruisce oggetto TitleBean
  17. public GuestDataBean() throws Exception {
  18.  
  19. Class.forName("com.mysql.jdbc.Driver");
  20.  
  21. // connessiona al DB
  22. c = DriverManager.getConnection("jdbc:mysql://localhost/guestbean?user=root&password=psw" );
  23.  
  24. s = c.createStatement();
  25. }
  26.  
  27. // ritorna lista ospiti
  28. public List getGuestList() throws SQLException {
  29. List guestList = new ArrayList();
  30.  
  31. // ottiene lista di ospiti
  32. ResultSet results = s.executeQuery(
  33. "SELECT firstName, lastName, email FROM guests" );
  34.  
  35. // ottinene dati dalle righe
  36. while ( results.next() ) {
  37. GuestBean guest = new GuestBean();
  38.  
  39. guest.setFirstName( results.getString( 1 ) );
  40. guest.setLastName( results.getString( 2 ) );
  41. guest.setEmail( results.getString( 3 ) );
  42.  
  43. guestList.add( guest );
  44. }
  45.  
  46. return guestList;
  47. }
  48.  
  49. // inserisce un ospite nel db
  50. public void addGuest( GuestBean guest ) throws SQLException {
  51. if(guest.getFirstName() == null || guest.getLastName() == null || guest.getEmail() == null) {
  52.  
  53. }else{
  54. s.executeUpdate( "INSERT INTO guests ( firstName, " +
  55. "lastName, email ) VALUES ( '" + guest.getFirstName() + "', '" +
  56. guest.getLastName() + "', '" + guest.getEmail() + "' )" );
  57. }
  58. }
  59.  
  60. // chiude istruzioni e termina la connessione col db
  61. protected void finalize()
  62. {
  63. // tenta di chiudere la connessione col db
  64. try {
  65. s.close();
  66. c.close();
  67. }
  68.  
  69. // gestisce SQLException nelle operazioni di chiusura
  70. catch ( SQLException sqlException ) {
  71. sqlException.printStackTrace();
  72. }
  73. }
  74.  
  75. public String visualizzaIscritti() {
  76. return "/guestBookView";
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement