Skip to content

Commit

Permalink
Conexão com o banco de dados melhorada & organização dos arquivos
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Maciel committed Oct 13, 2019
1 parent bc64d86 commit df2506a
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
11 changes: 5 additions & 6 deletions src/gustavo/com/github/conexao/Conexao.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ private static DataSource getDataSource() {
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("");
dataSource.setUrl("jdbc:mysql://localhost:3306/crud_java?useTimezone=true&serverTimezone=UTC&useSSL=false&rewriteBatchedStatements=true&relaxAutoCommit=true");
dataSource.setInitialSize(100);
dataSource.setMaxIdle(60);
dataSource.setMaxTotal(80);
dataSource.setPassword("Dal3qdal3!");
dataSource.setUrl("jdbc:mysql://localhost:3306/crud_java?useTimezone=true&serverTimezone=UTC&useSSL=false&rewriteBatchedStatements=true&relaxAutoCommit=true");
dataSource.setInitialSize(50);
dataSource.setMaxIdle(100);
dataSource.setMaxTotal(1000);
dataSource.setMaxWaitMillis(5000);
}

return dataSource;

}
public static Connection getConnection() throws SQLException {

return getDataSource().getConnection();

}
Expand Down
6 changes: 6 additions & 0 deletions src/gustavo/com/github/dao/TelefoneTipoDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public List<TelefoneTipo> listarTelefoneTipo() throws SQLException {

}catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return listaProdutos;
Expand Down Expand Up @@ -73,6 +76,9 @@ public List<TelefoneTipo> listarTelefoneTipoPorID(int id_telefone_tipo) throws S

} catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return arrayTelefoneTipo;
Expand Down
22 changes: 18 additions & 4 deletions src/gustavo/com/github/dao/UsuarioDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public long inserirUsuario(Usuario usuario) throws SQLException {

connection.commit();
statement.close();
connection.close();
} catch (SQLException e) {
connection.rollback();
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return id_gerado;
Expand All @@ -78,13 +80,15 @@ public boolean alterarUsuario(Usuario usuario) throws SQLException {
estadoOperacao = statement.executeUpdate() > 0 ;
connection.commit();
statement.close();
connection.close();


}
catch (SQLException e) {
connection.rollback();
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}


Expand All @@ -107,13 +111,15 @@ public boolean deletarUsuario(int id_usuario) throws SQLException {
estadoOperacao = statement.executeUpdate() > 0 ;
connection.commit();
statement.close();
connection.close();


}
catch (SQLException e) {
connection.rollback();
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}


Expand Down Expand Up @@ -145,10 +151,12 @@ public List<Usuario> listarUsuarios() throws SQLException {

}
statement.close();
connection.close();

}catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return listaUsuarios;
Expand Down Expand Up @@ -181,6 +189,9 @@ public Usuario listarUsuario(int id_usuario) throws SQLException {

} catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return u;
Expand Down Expand Up @@ -211,6 +222,9 @@ public Usuario login(Usuario u) throws SQLException {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return usuario;
Expand Down
11 changes: 9 additions & 2 deletions src/gustavo/com/github/dao/UsuarioTelefoneDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public boolean inserirUsuarioTelefone(UsuarioTelefone usuarioTelefone) throws SQ

connection.commit();
statement.close();
connection.close();
} catch (SQLException e) {
connection.rollback();
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return estadoOperacao;
Expand Down Expand Up @@ -75,6 +77,9 @@ public List<UsuarioTelefone> listarUsuarioTelefone(int id_usuario) throws SQLExc
resultSet.close();
}catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}

return arrayUsuarioTelefone;
Expand All @@ -95,13 +100,15 @@ public boolean deletarTodosOsNumerosDoUsuario(int id_usuario) throws SQLExceptio
estadoOperacao = statement.executeUpdate() > 0 ;
connection.commit();
statement.close();
connection.close();


}
catch (SQLException e) {
connection.rollback();
e.printStackTrace();
}finally{
System.out.println("fechou");
connection.close();
}


Expand Down
Binary file modified target/classes/gustavo/com/github/conexao/Conexao.class
Binary file not shown.
Binary file modified target/classes/gustavo/com/github/dao/TelefoneTipoDAO.class
Binary file not shown.
Binary file modified target/classes/gustavo/com/github/dao/UsuarioDAO.class
Binary file not shown.
Binary file modified target/classes/gustavo/com/github/dao/UsuarioTelefoneDAO.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Generated by Maven Integration for Eclipse
#Fri Oct 11 22:29:24 GFT 2019
#Sat Oct 12 18:55:29 GFT 2019
version=0.0.1-SNAPSHOT
groupId=CIDAJava
m2e.projectName=CIDAJava
m2e.projectLocation=C\:\\Users\\wpers\\eclipse-workspace\\CIDAJava
m2e.projectName=CRUD-Java
m2e.projectLocation=C\:\\Users\\wpers\\eclipse-workspace\\CRUD-Java
artifactId=CIDAJava

0 comments on commit df2506a

Please sign in to comment.