Java 将MySQL数据库连接到NetBeans错误
在NetBeans中运行代码以查看mySQL是否已连接时,我遇到了一个问题。代码如下:Java 将MySQL数据库连接到NetBeans错误,java,mysql,netbeans,Java,Mysql,Netbeans,在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","pa
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.ErrorArgumentException:格式错误的数据库URL,无法分析“=TRUE”附近的连接字符串。”
现在,为什么会这样?我想你需要补充一下
Class.forName(“com.mysql.jdbc.Driver”);
另外,确保正确设置了Connection conn=DriverManager.getConnection(字符串url、字符串用户、字符串密码);
中的所有内容
从代码中的url格式来看,就像您试图直接连接到数据库中的特定表tUsers
。我认为这不起作用。请纠正我的错误,无论它是否是您的数据库名称
因为我知道的基本代码应该是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");
}}}
希望可以完成这项工作。我建议从我想知道的是
jdbc:mysql://localhost:3306/tUsers?autoReconnect=true&useSSL=true
会更好谢谢你的提示,但是当我添加“Class.forName…”时,它说这是不必要的,“tUsers”是一个方案而不是一个表。