Código-Fonte da Prática 02 Sumário Classe ConexaoDB... 2 Classe PropriedadesBD... 5 Arquivo conexao.properties... 6 Programação Comercial Walderson Shimokawa 1
Classe ConexaoDB package br.edu.univag.util.sql; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.resultsetmetadata; import java.sql.sqlexception; import javax.swing.table.defaulttablemodel; public class ConexaoBD { private final String driver; private final String url; private final String usuario; private final String senha; private Connection con; private boolean conectado; public ConexaoBD() { this.driver = PropriedadesBD.DRIVER; this.url = PropriedadesBD.URL; this.usuario = PropriedadesBD.USUARIO; this.senha = PropriedadesBD.SENHA; public ConexaoBD(String driver, String url, String usuario, String senha) { this.driver = driver; this.url = url; this.usuario = usuario; this.senha = senha; public boolean isconectado() { return conectado; public void conectar() throws SQLException { try { this.conectado = false; Class.forName(driver); con = DriverManager.getConnection(url, usuario, senha); con.setautocommit(false); this.conectado = true; catch (ClassNotFoundException ex) { throw new SQLException(ex); public void desconectar() throws SQLException { if (isconectado() == true) { con.rollback(); con.close(); this.conectado = false; Programação Comercial Walderson Shimokawa 2
public void commit() throws SQLException { if (isconectado() == true) con.commit(); public PreparedStatement prepararsql(string sql) throws SQLException { return prepararsql(sql, false); public PreparedStatement prepararsql(string sql, boolean gerarchaves) throws SQLException { if (isconectado()) { if (gerarchaves) { return con.preparestatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); else { return con.preparestatement(sql, PreparedStatement.NO_GENERATED_KEYS); else { throw new SQLException("Não conectado ao banco de dados!"); public long getchave(preparedstatement ps) throws SQLException { long resposta = 0; try (ResultSet rs = ps.getgeneratedkeys()) { if (rs.next()) { resposta = rs.getlong(1); return resposta; //import javax.swing.table.defaulttablemodel; public DefaultTableModel gettabela(resultset rs) throws SQLException { DefaultTableModel tabela = new DefaultTableModel(); //Obtém os meta dados ResultSetMetaData rsmd = rs.getmetadata(); //Pega o número de colunas int numcolunas = rsmd.getcolumncount(); //Cria um vetor para guardar o título das colunas String[] nomesdascolunas = new String[numColunas]; //Alimenta o vetor com os títulos das colunas for (int i = 0; i < numcolunas; i++) { nomesdascolunas[i] = rsmd.getcolumnlabel(i + 1); //Ajusta os títulos das colunas da tabela em função dos //dados do vetor tabela.setcolumnidentifiers(nomesdascolunas); while (rs.next()) { Object[] linha = new Object[numColunas]; for (int i = 0; i < numcolunas; i++) { Programação Comercial Walderson Shimokawa 3
linha[i] = rs.getobject(i + 1); tabela.addrow(linha); return tabela; Programação Comercial Walderson Shimokawa 4
Classe PropriedadesBD package br.edu.univag.util.sql; import java.io.ioexception; import java.util.properties; import java.util.logging.level; import java.util.logging.logger; class PropriedadesBD { public static final String DRIVER; public static final String URL; public static final String USUARIO; public static final String SENHA; static { Properties props = new Properties(); try { props.load(properties.class.getresourceasstream("/conexao.properties")); catch (IOException ex) { Logger.getLogger(PropriedadesBD.class.getName()).log(Level.SEVERE, null, ex); DRIVER = props.getproperty("jdbc.driver"); URL = props.getproperty("jdbc.url"); USUARIO = props.getproperty("jdbc.usuario"); SENHA = props.getproperty("jdbc.senha"); Programação Comercial Walderson Shimokawa 5
Arquivo conexao.properties jdbc.driver = com.mysql.jdbc.driver jdbc.url = jdbc:mysql://localhost/pedido jdbc.usuario = root jdbc.senha = Programação Comercial Walderson Shimokawa 6