2

Hi so I'm starting to learn about database connections in JAVA and I'm developing a mini application which requires a database. I wanted to know what's the proper way of using the the Connection object so that I can use it with multiple methods.

A method to Add a new record, another to search for a specific key and so on. I will have to access multiple tables each having their own own class and set of methods.

If there's any good book out there about Software Development in JAVA, that might be useful too.

3
  • Sorry, your question in this format is off-topic here because of the rule: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." Commented Feb 20, 2016 at 12:20
  • Please don't repeat the common first-timer mistake of creating a Connection and using it for every query you send to the database. Commented Feb 20, 2016 at 12:27
  • what's the correct way then? It's all I'm asking Commented Feb 20, 2016 at 12:45

2 Answers 2

3

I think that for simple cases just open and close the connection on each query. For more complicated cases (like a server) you can use connection pool which keeps a list of opened connections

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

Comments

0

if the application is small you can create a connection object each time you hit the database but if the number of request to the database is pretty high you can use Connection pool you can use BasicDataSource which is an apache project use singleton pattern to wrapp the instance of basic dataSource and retrieve the connection Object each time you want from that wrapping class DataWrapper

import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

public class DataWrapper {

    private static DataSource datasource;

    private static void initialize() throws Exception {

        Properties props = new Properties();
        props.put("url", "jdbc:mysql://localhost:3306/testbd");
        props.put("password", "root");
        props.put("username", "root");
        props.put("driverClassName", "com.mysql.jdbc.Driver");
        props.put("initialSize", "10");
        props.put("maxTotal", "15");
        props.put("maxIdle", "10");
        props.put("minIdle", "5");
        datasource = BasicDataSourceFactory.createDataSource(props);

    }

    public static synchronized Connection getConnection() throws Exception {
        if (datasource == null) {
            initialize();
        }
        return datasource.getConnection();

    }

}

caller

Connection con=DataWrapper.getConnection();
    PreparedStatement statement=con.prepareStatement("select * from users");

    ResultSet result= statement.executeQuery();
    result.next();
    System.out.println(result.getString(1));
    //dont forget to close the connection object
    con.close();

Comments

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.