将mysql数据库连接到netbeans时出错

hm2xizp9  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(348)

在netbeans中运行代码以查看mysql是否已连接时,我遇到了一个问题。代码如下:

public static void main(String[] args) {
    Connection connect = null;

    try{
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/tUsers?autoReconnect=true/useSSL=TRUE","root","password");
        if(connect!=null)
        {
        System.out.println("Connected");
        }
    }catch (Exception e)
    {
        System.out.println("RIP");
    }
  }
}

当我运行它打印出“rip”。当我逐行调试它时,它从“connect=drivermanager.getconnection…”变成了“system.out.println(“rip”),当我看到“exception e”时,它说“e=(java.sql.sqlnontransientconnectionexception)java.sql.sqlnontransientconnectionexception:无法加载连接类,因为基础异常:com.mysql.cj.exceptions.wrongargumentexception:数据库url格式错误,未能解析'=true'附近的连接字符串。”
为什么会这样?????

o4hqfura

o4hqfura1#

我想你需要补充一下 Class.forName("com.mysql.jdbc.Driver"); .
还有,确保所有的东西都在里面 Connection conn = DriverManager.getConnection (String url, String user, String password); 设置正确。
从代码中的url格式来看,就好像您试图直接连接到特定的表 tUsers 在你的数据库里。我不认为这会起作用。如果我错了,请纠正我,不管它是否是你的数据库名称。
因为我知道的基本url格式应该是 jdbc:mysql://localhost:3306/yourDBname .
如果您已经正确设置了文章中所写的url,那么代码是

public static void main(String[] args) {

try{
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/tUsers?autoReconnect=true/useSSL=TRUE","root","password");
    if(connect!=null)
    {
    System.out.println("Connected");
    }
}catch (Exception e)
{
    System.out.println("RIP");
}}}

希望那能做这项工作。

相关问题