我想用下面的代码连接到Mysql数据库,但我的尝试失败。无法连接到本地主机上的MySQL
这是我的尝试:
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的时候记得输入密码“根”了,但是我不是100%肯定,如果用户名autmatically分配“根”,我真的很感激你的帮助。通过尝试登录到MySQL从命令shell com.mysql.jdbc.Driver
什么错误消息? – 2011-12-20 14:22:50
通常mysql连接在3306端口上。试试'url'。试试看'String url =“jdbc:mysql:// localhost:3306”;' 是的,mysql自动带有“root”用户 – sfat 2011-12-20 14:24:04
com.mysql.jdbc.Driver – 2011-12-20 14:24:30