Snake Snake - 3 months ago 23
Java Question

Correct design for querying DB through out the app

I want to use sqlite driver for my java application that I am developing with netbeans. What would be the correct "design" when it comes to integrating DB queries?

Basically should I create a static variable holding the connection which I can use to execute SQL statements through out the app? Or should I create the connection everytime I want to do the query?

Here is my code

conn = DriverManager.getConnection("jdbc:sqlite:Mydb.db");
st = conn.createStatement();
rs = st.executeQuery(/*My sql statement*/);

Thank you


Creating connection every time you write a query is not a good approach because it will be an overload on database for handling multiple connections.
Rather you should prefer a method or a class which would return you an instance of database connectivity.