04.06.2008 | 22:12:22
immagine
Ing. Emanuele Scapin

Java, es1, connessione a un database e query varie
  • Classe Database con metodi per la connessione, la sconnessione, l'inserimento di uno stato e la lettura dell'elenco di stati da un database con schema per la gestione dei dati degli stati.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Database {
        private String schema;
        private String username;
        private String password;
        private String host;
        private int port;
        
        private Connection conn = null;
    
        public Database() {
        }
    
        public Database(String schema, 
                String username, String password, 
                String host, int port) {
            this.schema = schema;
            this.username = username;
            this.password = password;
            this.host = host;
            this.port = port;
        }
        
        public void Connetti() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(
                 "jdbc:mysql://172.18.11.100:3306/test_personalita",
                        "prova3", "");   
            } catch (Exception ex) {
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            //conn.close();        
        }
        
        public void ConnettiEx() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(
                      "jdbc:mysql://"+host+":"+port+"/"+schema,username,password);            
            } catch (Exception ex) {
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            //conn.close();        
        }    
        
        public void Sconnetti() {
            if (conn != null) try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        public ArrayList<Stato> getElencoStati() {
            ArrayList<Stato> ret = new ArrayList<Stato>();
            if (conn != null) {
                try {
                   String sql = "select * from country order by name";
    
                   PreparedStatement psd = conn.prepareStatement(sql);
    
                   ResultSet rsd = psd.executeQuery();
    
                   if (rsd != null) {
                     while(rsd.next()) {
                         String name = rsd.getString("name");
                         String code = rsd.getString("code");
                         String region = rsd.getString("region");
                         int population = rsd.getInt("population");
    //                     System.out.println("name: "+name+
    //                             ", code: "+code+", region: "+region+
    //                             ", population: "+ population);
                         Stato st = new Stato(name, code, region, population);
                         ret.add(st);
                     }
                   }
                }
                catch(Exception ex) {
                    System.out.println("errore query stati " + ex.getMessage());
                }
            }
            
            return ret;
        }
        
        public int addStato(Stato stato) {
           int ret = -1;
    
           if (conn != null) {
                try {
    //               String sql = "insert into country (name) "+
    //                            "values ('"+stato.getNome()+"')";
                   String sql = "insert into country (name) "+
                                "values (?)";
                   
                   PreparedStatement psd = conn.prepareStatement(sql);
                   psd.setString(1, stato.getNome());
                   ret = psd.executeUpdate();
                }
                catch(Exception ex) {
                    System.out.println("errore query stati " + ex.getMessage());
                }
            }
            
            
            return ret;
        }
    }
            

© Emanuele Scapin 2008-2018

 

Valid HTML 4.01! Valid CSS!