GetConnection 始终返回 NULL 值,JAVA11

GetConnection always retun NULL value , JAVA11

我在 ubuntu 18.04.3

上使用 OpenJDK11 和 Netbeand IDE 11.0

我有一个方法可以将我的应用程序连接到我的数据库,就像这样

Connection connection ;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";

public boolean Conexion(String user,String pass){
    try {
        Class.forName(driver);
        connection = (Connection) DriverManager.getConnection(url,user,pass);
        System.out.println(connection);
        return true;
    } catch (ClassNotFoundException | SQLException ex) {
        return false;
    }
}

我还有一个获取连接的方法

public Connection getConnection(){
    return connection;
}

但是当我尝试这样做时

public class principal extends Conexion {
public static void main(String[] args) {
    if (new Conexion().Conexion("root", "")) {
        System.out.println(new Conexion().getConnection());
    }
}
}

我得到这个输出

run:
com.mysql.jdbc.JDBC4Connection@4141d797
null
BUILD SUCCESSFUL (total time: 2 seconds)

我使用连接方式作为登录方式, 用户有一个登录界面,这里必须输入用户名和密码,如果这个用户是在xampp服务器上注册的,就可以进入,可以看到,在例子中,我输入了用户名和默认密码 xampp ,起初它有效,但 getConnection 方法 returns 为空,我不明白为什么。

谢谢你们的时间。

考虑这段代码

if (new Conexion().Conexion("root", "")) {
    System.out.println(new Conexion().getConnection());
}

第一个 Conexion 对象与第二个对象不同 - 因此 getConnection() 的 return 值尚未初始化