简体   繁体   中英

How can I solve this Java Jdbc connection Error?

I have written a simple Java Jdbc connection program. I have put the MySQL connector jar. I have also put classpath in the environment variable. Still couldn't solve this connection error.

This is my code:

public class check {

    public static void main(String[] args) {

        System.out.println("-------- MySQL JDBC Connection Demo ------------");
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } 
        catch (ClassNotFoundException e) {
            System.out.println("MySQL JDBC Driver not found !!");
            return;
        }
        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;
        try {
            connection = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/company","root","");
            System.out.println("SQL Connection to database established!");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            return;
        } finally {
            try
            {
                if(connection != null)
                    connection.close();
                System.out.println("Connection closed !!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Output:

-------- MySQL JDBC Connection Demo ----------
MySQL JDBC Driver Registered!
Connection Failed! Check output console
Connection closed !!

Replace the following lines

} catch (SQLException e) {
    System.out.println("Connection Failed! Check output console");
    return;
}

with

} catch (SQLException e) {
    System.out.println("Connection Failed! Check output console");
    e.printStackTrace();
}

so that you can get the root cause of the issue. Your current code is simply eating that vital information.

On a side note, remove the following vestigial code:

try
{
    Class.forName("com.mysql.cj.jdbc.Driver");
} 
catch (ClassNotFoundException e) {
    System.out.println("MySQL JDBC Driver not found !!");
    return;
}

Class.forName("com.mysql.cj.jdbc.Driver") is not needed since JDBC 4.0.

Exceptions contain useful information. By catching them and printing 'error,'. you're eliminating this information.

This is not how you handle exceptions. Here's the rule: logging it / showing an error isn't handling it. So, don't.

Just declare 'throws Exception' on your main method and remove ALLLLL the try/catch stuff. Makes the code a heck of a lot more readable and now you get to see the full details of the error.

If you MUST catch these exceptions, the proper code for a catch block when you don't really know what to do is: throw new RuntimeException("Unhandled exception", e); - this still guarantees you see all the error messages and other details.

Once you do it this way, the error message will tell you what's going wrong.

To make the connection with JDBC you need to import the MySQL connector jar file into your project then the JDBC will make a connection with the database.

For this particular Exception which you are getting for that there are only few causes if you have installed MySql successfully. ( Class.forName("com.mysql.cj.jdbc.Driver") will not have any impact on your code) This causes are -

  1. You haven't created database company is not present in your MySql Database.
  2. User name for MySql client is not valid.
  3. Password for user root is not correct (You haven't provided)
  4. MySql server is not started in your system to connect client (Start it before login) for windows you can run this file \MySQL\MySQL Server 8.0\bin\mysqld.exe in order to start it.

Please check all this possibilities.

I have tried your code with my database it is working fine just check for possible causes.

I got it.. My wamp Mysql server url was localhost:3308.. But I wrote 3306 in my program... finally its solved..Thanks everyone for helping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM