9
I am creating a system in Netbeans, using the Java language and the Mysql database. I wrote the following code to make the connection between the program and the database:
public class Conexao
{
private static final String DRIVER="com.mysql.jdbc.Driver",URL="jdbc:mysql://localhost:3306/banco_dados",USUARIO="root",SENHA="root";
public static Connection obter()
{
try
{
Class.forName(DRIVER);
return DriverManager.getConnection(URL,USUARIO,SENHA);
}
catch (ClassNotFoundException|SQLException ex)
{
JOptionPane.showMessageDialog(null,"Erro ao estabelecer conexão com o MySQL.");
}
return null;
}
public static void fechar(Connection c)
{
try
{
if(c!=null)
c.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Erro ao fechar conexão com o MySQL.");
}
}
public static void fechar(Connection c, PreparedStatement ps)
{
fechar(c);
try
{
if(ps!=null)
ps.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Erro ao fechar conexão com o MySQL.");
}
}
public static void fechar(Connection c, PreparedStatement ps, ResultSet rs)
{
fechar(c,ps);
try
{
if(rs!=null)
rs.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Erro ao fechar conexão com o MySQL.");
}
}
}
For class Usuario
, created the class UsuarioDAO
. I will still create methods for reading, editing and deletion.
public class UsuarioDAO
{
public static void inserir(Usuario usuario)
{
Connection c=Conexao.obter();
PreparedStatement ps=null;
try
{
ps=c.prepareStatement("insert into usuarios(nome,senha) values(?,?)");
ps.setString(1,usuario.getNome());
ps.setBytes(2,usuario.getSenha());
ps.executeUpdate();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Erro ao inserir dados no MySQL.");
}
finally
{
Conexao.fechar(c,ps);
}
}
}
Regarding the above classes, I have the following doubts:
It is necessary to create the class
Conexao
and the classesDAO
manually or there is some functionality in Netbeans that can configure the program’s connection to the database using a form or something like?Is it necessary to open and close the connection every time you make a query or change in the database? Why?
It is necessary to create three overloads of the method
fechar
in classConexao
? Why?
Nice use of Try-with-Resources, but I have a question. Where the connection is closed on it?
– DiegoAugusto
Class.forName(DRIVER);
I believe that it is no longer necessary, since java6(jdbc 4), if the bank driver is part of the classhpath, it is located automatically.http://stackoverflow.com/a/28220844/5524514– user28595
@Diegoaugusto With the syntax of Try-with-Resources, compiler inserts a block on its own Finally which closes all objects that are opened at the beginning of Try (in the part between the parentheses). The purpose of this syntax is exactly to rid the programmer of the need and complexity of explicitly closing open resources.
– Victor Stafusa
Got it, pretty cool, I didn’t know.
– DiegoAugusto
@diegofm Response edited. Thank you. :)
– Victor Stafusa
@diegofm, I did not understand the answer to topic 1, I do not know if it is the case, but why the issue of the graphical interface knowing the database goes against the MVC. I believe the issue is regarding the automation of the IDE in completing the connection parameters.
– Marcos Sousa
@Marcossousa Because the database is something that is at the base of the model, probably in a layer below that of services and business rules and encapsulated in it. If the model exposes access to the database directly to the visualization layer, this indicates a break of the encapsulation, which implies a bad architecture.
– Victor Stafusa
@Victorstafusa, I understood this concept(thank you), but I don’t know if I got it right, but I thought the question referred to Netbeans setting up the connection, something like creating a connection and setting it up with the proper parameters. and it was a practice that I used a lot, but I never understood the problem of encapsulation. Anyway it is a personal doubt.
– Marcos Sousa
Victor, regarding 1, I understand that he wants to know if there is any help in the Netbeans IDE for creating the connection classes and Daos.
– Raimundo Norberto
Regarding the first question, I was asking about the existence of some functionality (for example, a form) that automates the creation of the class
Conexao
and of the classesDAO
in Netbeans. I’ve already edited the question.– Marcelo Henrique Bittencourt
@Marcelohenriquebittencourt Look at this: http://plugins.netbeans.org/plugin/60454/sql-dal-maker
– Victor Stafusa
@Raimundonorberto Edited reply.
– Victor Stafusa
@Victorstafusa, thanks for the clarifications.
– Marcelo Henrique Bittencourt
Another question: If the method throws exception instead of catching it, the Try-with-Resources no longer necessary. Objects will be closed anyway?
– Marcelo Henrique Bittencourt
@Marcelohenriquebittencourt If the method throws exception, it will try to execute the Finally which the compiler puts. This means that it will also try to close the resources even if an exception is thrown.
– Victor Stafusa
Okay, thank you again.
– Marcelo Henrique Bittencourt