Problem in calling DriveManager.getConnection?

3

I'm studying Java with database and I'm doing a few examples. On the PC with Windows I used a code and it worked, there was no problem, I now use Linux and the same code is giving error when calling the% with% of DriveManager.getConnection .

Follow the code below.

package br.com.utd.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Connection;

public class Conexao {
    private final static String URL = "jdbc:mysql://127.0.0.1:3306/livraria";
    private final static String USUARIO = "root";
    private final static String SENHA = "**********";

    public static Connection factoryConexao(){
        Connection conexao = null;
        try{
            conexao = DriverManager.getConnection(URL, USUARIO, SENHA);
            return conexao;
        }catch(SQLException e1){
            JOptionPane.showMessageDialog(null,  
                       "Falha na conexão com o banco de dados:"+e1);
            return conexao;
        }
    }

}

The error is as follows Eclipse suggests adding a cast with JDBC to:

conexao = DriverManager.getConnection(URL, USUARIO, SENHA);

I can not understand why to make a Connection with cast if it's a Connection ?

    
asked by anonymous 23.01.2017 / 13:50

1 answer

2
  

I can not figure out why cast with Connection if it's a Connection?

This happens because you have import wrong ( import com.mysql.jdbc.Connection; ) in your code the correct one would be imports java.sql.Connection; , the others are correct.

The error in the image is saying different types, and Eclipse is trying to sort out your code by giving cast , but the problem really was that imports is wrong for Connection .

The correct imports are:

import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane; // esse é o único que não faz parte do problema

23.01.2017 / 14:04