5
I have a class called JavaConnect
and in it I have two methods: ConnectDB
and DesconnectDb
.
Connection class:
public class JavaConnect {
Connection conn = null;
public static Connection ConnectDb(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:/home/leo/NetBeansProjects/Controller_workshop/dbControlworkshop.sqlite");
/*JOptionPane.showMessageDialog(null, "Conexao realizada");*/
return conn;
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
public static void DesconnectDb(Connection conn){
try{
if(conn != null) {
conn.close();
}
}catch (SQLException e){
}
}
}
Until then I understood more or less the functionalities. However I need to start them inside Jframe.
Code below to help understand my mistake:
public void PreencherTabela(String Sql){
ArrayList dados = new ArrayList();
String [] colunas = new String []{"ID","NOME","RG","CPF","RUA","TELEFONE","NUMERO","BAIRRO","CIDADE","ESTADO","CEP"};
JavaConnect.ConnectDb();
JavaConnect.DesconnectDb(conn);
}
When I call the method DesconnectDb
, the "Conn" gets wrong and presents me the error on the side as follows:
cannot find Symbol
How to solve this?
Calling this.Conn will not give error in a static method? It would work if it were a variable of the object, in which case Javaconnect should be instantiated
– Denis Rudnei de Souza
@Denisrudneidesouza is true, is the hehe habit. I will change
– user28595
Thanks for the guidance, your explanation helped me understand the problem!
– Leo Bufalo
I understand that the answer is useful and helps, but for those who access this question in the future, it is good to know that this is not a recommended structure to access the database, as it can easily occur the case of not properly close the connection. My answer proposes two safer mechanisms.
– utluiz