Java 无法连接到我的 SQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20324476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant connect to my SQL database
提问by Chris Phillips
So I'm having an issue connecting to MySQL with Java. Heres my code:
所以我在使用 Java 连接到 MySQL 时遇到了问题。这是我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBAccess {
private String Host;
private String User;
private String Password;
public DBAccess() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Password = "password";
Host = "jdbc:mysql://localhost/worlddb";
User = "root";
@SuppressWarnings("unused")
Connection connect = DriverManager.getConnection(Host,User,Password);
System.out.println("Connected!");
}
public void RetreiveData(){
}
public void ChangeData(){
}
}
The error that I'm getting is Exception in thread "main"
我得到的错误是线程“main”中的异常
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'worlddb'
http://postimg.org/image/593stjvjx/in mySQL workbench, my connection name is "worlddb" hostname is Liquidus(which is the localhost)
http://postimg.org/image/593stjvjx/在 mySQL 工作台中,我的连接名称是“worlddb” 主机名是 Liquidus(这是本地主机)
- socket is MySQL
- Port: 3306
- 套接字是 MySQL
- 端口:3306
Why is this?
为什么是这样?
采纳答案by Charaf JRA
There is a difference between name of connection and name of Database,try world, testor sakilafrom schemas in the picture.
图中的模式中的连接名称和数据库名称、尝试世界、测试或sakila之间存在差异。
回答by Nidhish Krishnan
Make sure that the database what you have mentioned in Host = "jdbc:mysql://localhost/worlddb";
i.e worlddbis correct or not.
The name of the database here is CASE SENSITIVE!So, if you try to connect using a database name like "test2" instead of "Test2" , you will get the
确保您在Host = "jdbc:mysql://localhost/worlddb";
ie worlddb 中提到的数据库是否正确。这里的数据库名称是区分大小写的!因此,如果您尝试使用诸如“ test2”而不是“ Test2”之类的数据库名称进行连接,您将获得
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test2'
Also try using port number too
也尝试使用端口号
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/worlddb", "root", "password");
回答by Angelo Immediata
Are you sure that the DB server is not case sensitive? I mean maybe on the DB server the DB name is WorldDb and you are trying to connect to it by using worlddb (all low lecters).. Try to use the same name or to configure MySQL to be case insensitive
您确定数据库服务器不区分大小写吗?我的意思是,也许在数据库服务器上,数据库名称是 WorldDb,而您正尝试使用 worlddb(所有低语者)连接到它。尝试使用相同的名称或将 MySQL 配置为不区分大小写
回答by Mr.G
Try this it's hopeful for you:
试试这个它对你有希望:
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con =DriverManager.getConnection("jdbc:mysql://localhost/worlddb","root","password");
回答by Eng.Huda
connection = DriverManager.getConnection("jdbc:mysql://localhost/worlddb?" +"user=root&password=password");
Try this, I have a connection to mysql db with same connection, you just add ?
after the name of the db.
试试这个,我有一个连接到mysql db的相同连接,你只需?
在db的名称后面添加。
回答by salma
In the above code, port number is missing.
在上面的代码中,缺少端口号。
connection = connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/worlddb","root", "password");
default port number of mysql is 3306
mysql 的默认端口号是 3306
回答by ar blouch
Write
写
"jdbc:mysql://localhost:3306/worlddb"
instead of
代替
"jdbc:mysql://localhost/worlddb"
回答by Michael Toth
Charaf jra was right i had the same issue.
your schema(schema = Database) is listed as Host = "jdbc:mysql://localhost/worlddb";If you look at the schemas in the pic you posted https://postimg.cc/image/593stjvjx/
the schema(Database) is world.
Charaf jra 是对的,我有同样的问题。
您的架构(架构 = 数据库)被列为 Host = "jdbc:mysql://localhost/worlddb"; 如果您查看您发布的图片中
的架构https://postimg.cc/image/593stjvjx/
架构(数据库)是world。
回答by David Caceres
Take care to not have white spaces and use jdbc:mysql://localhost:3306/dbname
注意不要有空格并使用 jdbc:mysql://localhost:3306/dbname