Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package modello;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- public class GuestDataBean {
- private Connection c;
- private Statement s;
- // costruisce oggetto TitleBean
- public GuestDataBean() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- // connessiona al DB
- c = DriverManager.getConnection("jdbc:mysql://localhost/guestbean?user=root&password=psw" );
- s = c.createStatement();
- }
- // ritorna lista ospiti
- public List getGuestList() throws SQLException {
- List guestList = new ArrayList();
- // ottiene lista di ospiti
- ResultSet results = s.executeQuery(
- "SELECT firstName, lastName, email FROM guests" );
- // ottinene dati dalle righe
- while ( results.next() ) {
- GuestBean guest = new GuestBean();
- guest.setFirstName( results.getString( 1 ) );
- guest.setLastName( results.getString( 2 ) );
- guest.setEmail( results.getString( 3 ) );
- guestList.add( guest );
- }
- return guestList;
- }
- // inserisce un ospite nel db
- public void addGuest( GuestBean guest ) throws SQLException {
- if(guest.getFirstName() == null || guest.getLastName() == null || guest.getEmail() == null) {
- }else{
- s.executeUpdate( "INSERT INTO guests ( firstName, " +
- "lastName, email ) VALUES ( '" + guest.getFirstName() + "', '" +
- guest.getLastName() + "', '" + guest.getEmail() + "' )" );
- }
- }
- // chiude istruzioni e termina la connessione col db
- protected void finalize()
- {
- // tenta di chiudere la connessione col db
- try {
- s.close();
- c.close();
- }
- // gestisce SQLException nelle operazioni di chiusura
- catch ( SQLException sqlException ) {
- sqlException.printStackTrace();
- }
- }
- public String visualizzaIscritti() {
- return "/guestBookView";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement