无法连接到本地主机上的 MySQL [英] can't connect to MySQL on localhost
本文介绍了无法连接到本地主机上的 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用下面的代码连接到 Mysql 数据库,但我的尝试失败了.
i am trying to connect to Mysql database using the code below , yet my attempt fails.
这是我的尝试:
private static Connection conn = null;
private static String url = "jdbc:mysql://localhost/";
private static String dbName = "proj1";
private static String driver = "com.mysql.jdbc.Driver";
private static String userName = "root";
private static String password = "root";
public static int setupConnection ()
{
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,"root","root");
return 1;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
return 0;
}
}
在安装 MySQL 时,我记得输入了密码root",但我不能 100% 确定用户名是否是自动分配的root",我非常感谢您的帮助.
when installing MySQL i remember entering the password "root" , but im not 100% sure if the username is autmatically assigned "root" , i really appreciate your help.
我收到错误信息:com.mysql.jdbc.Driver
i get the error message : com.mysql.jdbc.Driver
推荐答案
您需要将 MySQL Connector/J 驱动程序添加到您的 Netbeans 项目的 build-path/classpath.否则无法加载.
You need to add the MySQL Connector/J driver to the build-path/classpath of your Netbeans project. Otherwise it cannot be loaded.
这篇关于无法连接到本地主机上的 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文