I can not connect to my SQL database

So, I have a problem connecting to MySQL with Java. Here is my code:

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 I get is the exception in the "main" thread

 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 local host)

  • socket is MySQL
  • Port: 3306

Why is this?

+7
java mysql jdbc
source share
9 answers

There is a difference between the connection name and the database name, try the world , test or sakila from the diagrams in the picture.

+7
source share

Make sure the database you mentioned in Host = "jdbc:mysql://localhost/worlddb"; ie worlddb , true or not. The name of the database here is CASE SENSITIVE! So, if you try to connect using a database name such as " test2 " and not " Test2 ", you will get

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test2' 

Also try using a port number

 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/worlddb", "root", "password"); 
+1
source share

Try this for you:

 Connection con = null; Class.forName("com.mysql.jdbc.Driver"); con =DriverManager.getConnection("jdbc:mysql://localhost/worlddb","root","password"); 
+1
source share
 connection = DriverManager.getConnection("jdbc:mysql://localhost/worlddb?" +"user=root&password=password"); 

Try this, I have a connection to mysql db with the same connection, are you just adding ? after the name db.

+1
source share

In the above code, the port number is missing.

 connection = connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/worlddb","root", "password"); 

The default port number for mysql is 3306

+1
source share

Are you sure the database server is not case sensitive? I mean, perhaps on the database server the database name is WorldDb, and you are trying to connect to it using worlddb (all low-level). Try to use the same name or configure MySQL to be case insensitive.

0
source share

Write

 "jdbc:mysql://localhost:3306/worlddb" 

instead

 "jdbc:mysql://localhost/worlddb" 
0
source share

Charaf jra was right, I had the same problem.
your schema (schema = database) is listed as Host = "jdbc: mysql: // localhost / worlddb"; If you look at the diagrams in the figure, you sent https://postimg.cc/image/593stjvjx/
schema (database) world .

0
source share

Be careful that there are no spaces and use jdbc:mysql://localhost:3306/dbname

0
source share

All Articles