import java.sql.*; /** * * @author Murilo Adriano */ public class AcessoDados { private Statement dbVar; /** Creates a new instance of AcessoDados */ public AcessoDados() { try { conectarDb(); criarTabelas(); } catch (SQLException e) { e.printStackTrace(); } } private void conectarDb() throws SQLException { if (dbVar == null) { try { Class.forName("org.hsqldb.jdbcDriver"); } catch (Exception e) { System.out.println("Erro ao carregar o driver JDBC."); } Connection con = DriverManager.getConnection("jdbc:hsqldb:file:lib/dados/db", "sa", ""); dbVar = con.createStatement(); } } private void criarTabelas() { try { dbVar.executeQuery("SELECT * FROM clientes LIMIT 1"); } catch (SQLException e) { if (e.getErrorCode() == -22) { try { dbVar.executeQuery( "CREATE TABLE clientes (" + "id INTEGER IDENTITY NOT NULL ," + "nome VARCHAR NOT NULL ," + "telefone INTEGER NOT NULL ," + "endereco VARCHAR NOT NULL ," + "setor VARCHAR NOT NULL ," + "observacoes VARCHAR NULL )" ); } catch (SQLException ex) { ex.printStackTrace(); } } } try { dbVar.executeQuery("SELECT * FROM setores LIMIT 1"); } catch (SQLException e) { if (e.getErrorCode() == -22) { try { dbVar.executeQuery( "CREATE TABLE setores ( " + "id INTEGER IDENTITY NOT NULL ," + "setor VARCHAR NOT NULL ," + "taxa INTEGER NOT NULL )" ); } catch (SQLException ex) { ex.printStackTrace(); } } } } public String[] buscarCliente(int telefone) throws SQLException { ResultSet resultado = dbVar.executeQuery( "SELECT * FROM clientes WHERE telefone LIKE '" + telefone + "%' limit 1" ); if (resultado.next()) { int rsId = resultado.getInt("id"); String rsNome = resultado.getString("nome"); int rsTelefone = resultado.getInt("telefone"); String rsEndereco = resultado.getString("endereco"); String rsSetor = resultado.getString("setor"); String rsObservacoes = resultado.getString("observacoes"); String rsIdString = Integer.toString(rsId); String rsTelefoneString = Integer.toString(rsTelefone); String[] rs = { rsIdString, rsNome, rsTelefoneString, rsEndereco, rsSetor, rsObservacoes }; return rs; } else { return null; } } public boolean cadastrarCliente(String nome, int telefone, String endereco, String setor, String observacoes) throws SQLException { ResultSet rs = dbVar.executeQuery("SELECT * FROM clientes WHERE telefone ='" + telefone + "'"); if (!rs.next()) { dbVar.executeQuery("INSERT INTO clientes VALUES (null," + "'" + nome + "', " + "'" + telefone + "', " + "'" + endereco + "', " + "'" + setor + "', " + "'" + observacoes + "'" + ")"); return true; } else { return false; } } public String[] obterSetores() throws SQLException { ResultSet count = dbVar.executeQuery("SELECT COUNT(*) numLinhas FROM setores"); count.next(); int numLinhas = count.getInt("numLinhas"); ResultSet rs = dbVar.executeQuery("SELECT * FROM setores ORDER BY setor"); int i = 0; String[] dados = new String[numLinhas]; while (rs.next()) { dados[i] = rs.getString("setor"); i++; } return dados; } public String cadastrarSetor(String setor, int taxa) { try { dbVar.executeQuery("INSERT INTO setores VALUES (null, '" + setor + "', '" + taxa + "')"); return "Setor cadastrado com sucesso!"; } catch (SQLException e) { e.printStackTrace(); return "Setor não cadastrado.\nCódigo do erro: " + e.getErrorCode() + "\n Erro: " + e.getMessage(); } } }