preface

Hello everyone, I am Bai Mo, a love of learning and swimming contradiction.

The DBUTlis class used to run the test threw an exception, which took me a long time. Keep a record of what went wrong.


Problems arise

The exceptions are as follows:

java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.

The project configuration is as follows: first is the DBUTil class for testing:

public class DBUtil {
    static DataSource ds = new ComboPooledDataSource();

    public static Connection getConn(a) throws SQLException {
        return ds.getConnection();
    }

    public static DataSource getDataSource(a) {
        return ds;
    }

    public static void main(String[] args) {

        try {
            System.out.println(getConn());
        } catch(SQLException e) { e.printStackTrace(); }}}Copy the code

Then there is the JAR package:


The solution

Let’s ignore the above line error content, all check again. == You can look at 4 and 5 first. = =

  1. Checking DBUTil’s code, which is a bit redundant but error-free, can be used to test for connection success.
  2. Check if the XML configuration file name is incorrect and is the same as the lib package.

The name of the file is c3P0-config. XML and the file is one level below the SRC directory. 3. Check whether the JAR package is imported.

  1. Check whether the version of the JAR that mysql connects to Java is the same as the version of the database. Here we go!

Log in to the database and run mysql -u root -p to check the database version

  1. Check the configuration in the XML file. After checking, you found that the contents in the box do not apply to mysql8.0 and need to be modified.

The two lines in the box are modified as follows:

<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/t2? useSSL=false&amp;serverTimezone=UTC</property>
Copy the code

Now you’re done and connected to the database successfully. Thanks so much for helping me with this blog!


conclusion

  1. The database driver JAR package should correspond to the database version;
  2. For versions higher than MySQL8.0, add time zone parameter for URL connection.
  3. 8.0 or more driver, driver class selection in the middle of a morecj, i.e.,com.mysql.cj.jdbc.Driver

Well, that’s the end of this article! Why don’t you click on it? Why don’t you click on it? Click follow

In the public account [White mo is a program ape] reply [information] to get all kinds of tutorial, software, e-book resources I have collected for many years ~ (collect = learn)