Não estou conseguindo conectar ao banco, quando vou realizar qualquer query ocorre o erro NullPointerException (linha 24 - ConnectionPool e 16 - TestaListagem
Segue abaixo as classes ConnectionPool e a classe TestaListagem:
package br.com.caelum.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class ConnectionPool {
private DataSource dataSource;
ConnectionPool() {
MysqlConnectionPoolDataSource pool = new MysqlConnectionPoolDataSource();
pool.setUrl("jdbc:mysql://localhost:3306/loja_virtual?useSSL=false");
pool.setUser("root");
pool.setPassword("123456");
}
public Connection getConnection() throws SQLException {
System.out.println("Abrindo conexão com o banco de dados");
Connection connection = dataSource.getConnection();
return connection;
}
}
package br.com.caelum.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestaListagem {
public static void main(String[] args) throws SQLException {
ConnectionPool conexao= new ConnectionPool();
try (Connection connection = conexao.getConnection()) {
String sql = "select * from Produto";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
boolean resultado = statement.execute();
ResultSet resultSet = statement.getResultSet();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String nome = resultSet.getString("nome");
String descricao = resultSet.getString("descricao");
System.out.println("id=" + id + ", nome=" + nome + ", descricao=" + descricao);
}
resultSet.close();
}
}
}
}