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)
Why is this?
Take care to not have white spaces and use jdbc:mysql://localhost:3306/dbname
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With