Why do I get ClassNotfoundException in this static method?

Viewed 14

I get ClassNotfoundException in this static method. When I run a test method, I can get class object, it runs correctly.

I think this may caused by static key word. Is this correct? How can I fix this?

Below is my code.

Test and right code. I don't get "not found" output.

@Test
public void test() {
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=utf-8","root","123456");
        System.out.println(connection);
    } catch (Exception e) {
        System.out.println("not found");
        e.printStackTrace();
    }
}

Wrong code. You can see that I get a ClassNotFoundException. enter image description here

0 Answers
Related