0

Button -> conectaDB(Connect DB) -> inseredadosBD(Insert data into db)

how can i return the connection to the inseredadosBD, to insert from the method?

Button event:

public void inseredadosBD(String grupo, int protocolo, String dataentrada, String escrevente, String auxiliar, String datalimite) {
    conectaBD();
-> here i will use the insert, i was planning to use something like conn.execute.. from here, but
}

i tryed conectaBD(Connection conectado);, tryed conectaBD(conectado);

to do something like conectado.execute.., but dont worked :/

Method conectaBD

public class metodos {
    public Connection conectado;
    public Connection conn;
    public Connection conectaBD() {
        //metodo responsavel por fazer a conexão com o BD
        try {
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection("jdbc:sqlite:c:\\grupo1.db");
//      Statement stm = conn.createStatement();  

        if (conn != null) {
            System.out.println("Conectou" );
//          fechaconexaoBD(conn);
        } else {
            System.out.println("Conexão Fail");
        }
        } catch (SQLException e) {
            System.out.println("Erro de conexão " );
            e.printStackTrace();
        }   
        return conn;
    }

..here go the rest of the code...

forget the //, i was just trying/testing things :s

1 Answer 1

1

You may also want to include the error you are getting.

i tryed conectaBD(Connection conectado);, tryed conectaBD(conectado);

These will not work as you have no method conectaBD defined to take a Connection as an argument.

Edit As per your comment,

yes i have method conectaBD... its in the first code block !! isnt it a method because the "Connection"?? the problem is this, how can i pass the connection to the inseredadosBD, this is my question :

yes you do have the method conectaBD() but you are trying to call a method conectaBD(Connection) which you have not defined

But I can suggest several options:

  1. Put your inseredadosBD() method within your metodos class, so you have access to your Connection variables. So then after calling conectaBD you would want to use conn as your connection

  2. Pass the connection as an argument to your inseredadosBD() method then use that connection. Like inseredadosBD(String.... , Connection c) Edit if you do this then you do not call conectaBD() inside this method, call it before calling this method and then call this method like this inseredadosBD(/*data you want to insert*/, conn) then in this method your Connection is called c, so you can use the conneciton Object c like below

    public void inseredadosBD(String grupo, int protocolo, String dataentrada, String escrevente, String auxiliar, String datalimite, Connection c) {
        c.//What ever connection method you want to use
          //here i will use the insert, i was planning to use something like conn.execute.
     }
    
  3. Instantiate your metodos class within your... not very pretty tho

Sign up to request clarification or add additional context in comments.

5 Comments

yes i have method conectaBD... its in the first code block !! isnt it a method because the "Connection"?? the problem is this, how can i pass the connection to the inseredadosBD, this is my question :S
See edit to number 2, like that. Then pass connection in when you call inseredadosBD... just like any other Object in java
i tryed adding return conn; | and than adding conectaDB(conectado);... but return that its not applicable to this arguments (Connection)
I've edited again (for the final time). If you cannot understand what I'm telling you then I'm sorry but you need to learn to understand the basics of Java programming better
yes i understood now... but i made a global variable, out of the conectaBD Connection conn = null; so i can access if anywhere in the class, thank the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.