Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant connect to my SQL database

Tags:

java

mysql

jdbc

So I'm having an issue connecting to MySQL with Java. Heres 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 that I'm getting is Exception in thread "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)

  • socket is MySQL
  • Port: 3306

Why is this?

like image 777
Chris Phillips Avatar asked Dec 02 '13 09:12

Chris Phillips


2 Answers

Take care to not have white spaces and use jdbc:mysql://localhost:3306/dbname

like image 129
David Caceres Avatar answered Oct 18 '22 21:10

David Caceres


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.

like image 23
Eng.Huda Avatar answered Oct 18 '22 22:10

Eng.Huda