0

I need make a connection between java web and mysql, I am using netbeans but I don't know how.

If I dont write the password I have this fail but if I write the password, the failure is java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

I need help with my code!, this is my code, I am using netbeans!

package conexion;
import java.sql.*;

public class CONEXION_BD {
    public static Connection getConexion(){
        Connection con=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection
                ("jdbc:mysql://localhost/bd_prueba?user=root&password=");

            System.out.println("Conexion ok");
        }catch(ClassNotFoundException | SQLException e){
            System.out.println("Error "+e);
        }
        return con;
    }

    public static void main (String[] args){
        CONEXION_BD.getConexion();
    }
}
JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • Please edit your question and write in english – Pritam Banerjee May 26 '20 at 03:07
  • @Anair Terán looks like the password value is missing in the declaration. – MauricioTL May 26 '20 at 03:14
  • @PritamBanerjee I need make a connection beetwen java web and mysql, i am using netbeans but i dont kwon how. if i dont write the password i have this fail but if write the password the fail is "java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long" *maybe i have fail wrinting in english, sorry, my english is basic! * – Anair Terán May 26 '20 at 05:12
  • Try this with proper user name and password `Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql","root","password");` – Pritam Banerjee May 26 '20 at 07:25
  • Can you share the full and exact error message? Usually, Java errors contain a stactrace that helps to locate the problem – Nico Haase May 26 '20 at 08:53
  • Upgrade your version of MySQL Connector/J. – Mark Rotteveel May 26 '20 at 08:58

2 Answers2

0

You should add this in MySQL to make sure that the user has the correct permissions:

CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';

And then GRANT ALL permissions:

GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost';

And then you can use DriverConnection like this and make sure that you have the username and password added correctly:

 Connection con = DriverManager.getConnection(  
                "jdbc:mysql://localhost:3306/mysql","root","password");  
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 1
    Please share some explanation such that others can learn from your answer - which exact part makes the difference? – Nico Haase May 26 '20 at 08:54
0

Is the reason for not claiming the port?

Or reset your database user information, it is recommended that you set a password to try, you have no problem with this code, please confirm the database status

package conexion;
import java.sql.*;

public class CONEXION_BD {
    public static Connection getConexion(){
        Connection con=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection
                ("jdbc:mysql://localhost:3306/bd_prueba?user=root&password=");

            System.out.println("Conexion ok");
        }catch(ClassNotFoundException | SQLException e){
            System.out.println("Error "+e);
        }
        return con;
    }

    public static void main (String[] args){
        CONEXION_BD.getConexion();
    }
}