GeorgeDopeG
GeorgeDopeG

Reputation: 175

Cannot instantiate DataSource

I am trying to make a DataSource object just as mentioned in documentation: Apache Tomcat 8.5 Connection Pool Code example.

My code is as follows:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.sql.DataSource;

import org.apache.tomcat.jdbc.pool.PoolProperties;

/**
 * Class for encapsulating data connection and retrieving.
 * @author George
 *
 */
public class DataConnector {

    private static final String USERNAME = "root";
    private static final String PASSWORD = "";
    private static final String DATABASE_NAME = "store_db";

    public DataConnector(){
        PoolProperties p = new PoolProperties();
        p.setUrl("jdbc:mysql://localhost:3306/" + DATABASE_NAME);
        p.setDriverClassName("com.mysql.jdbc.Driver");
        p.setUsername(USERNAME);
        p.setPassword(PASSWORD);

        DataSource ds = new DataSource();

    }

}

But it gives the following error: Cannot instantiate the type DataSource. Any suggestions why?

Upvotes: 1

Views: 3110

Answers (1)

JB Nizet
JB Nizet

Reputation: 692191

The example code uses org.apache.tomcat.jdbc.pool.DataSource, which is not the same class as the one you're using: javax.sql.DataSource.

Upvotes: 1

Related Questions