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","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' 附近的连接字符串。”
现在,这是为什么呢????
解决方案
我认为您需要添加
Class.forName("com.mysql.jdbc.Driver");
.
另外,请确保所有内容Connection conn = DriverManager.getConnection (String url, String user, String password);
都正确设置。
从代码中的 url 格式来看,就像您试图直接连接到tUsers
数据库中的特定表一样。而且我认为这行不通。如果我错了,请纠正我,无论它是否是您的数据库名称。
因为我知道的基本url 格式应该是jdbc:mysql://localhost:3306/yourDBname
.
如果您已经按照帖子中的说明正确设置了网址,则代码为
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");
}}}
希望可以完成这项工作。
推荐阅读
- java - 在边界之间查找数组中所有整数的最快方法
- javascript - 带有 PHP 和单独服务器的聚合物
- amazon-web-services - 我无法使用 AWS AD 凭证连接到 AWS 实例
- vue.js - vee-validate 的基本示例不起作用
- java - 将节点解析为 FXML
- google-sheets - 从单元格中的逗号分隔列表中删除重复的字符串
- linux - jq 是否可以在设置新值时使用已删除的值?
- javascript - 按相等的两个键对对象数组进行排序
- laravel - 雄辩,从嵌套关系查询中检索结果
- react-native - 使用原生模块通过 JS 进行 HTTP 调用的性能优势?