package jdbc; import java.util.logging.Level; import java.util.logging.Logger; import java.sql.*; public class AcessoBD { private Connection conn; private Statement stm; private ResultSet rs = null; public AcessoBD(String sgbd, String bd, String usuario, String senha) { if (sgbd.equals("mysql")) { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/" + bd + "?user=" + usuario); } catch (SQLException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } } if (sgbd.equals("derby")) { try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); conn = DriverManager.getConnection("jdbc:derby://localhost:1527/" + bd, usuario, senha); } catch (SQLException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } } try { stm = conn.createStatement(); } catch (SQLException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } } public ResultSet select (String sql) { try { rs = stm.executeQuery(sql); } catch (SQLException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } return rs; } public int update (String sql) { int r = 0; try { r = stm.executeUpdate(sql); } catch (SQLException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } return r; } public int delete (String sql) { return (this.update(sql)); } public void fechar() { try { conn.close(); } catch (SQLException ex) { Logger.getLogger(AcessoBD.class.getName()).log(Level.SEVERE, null, ex); } } }