Search code examples
javamysqljdbc

JDBC connection without database definition


I have a class wich connects to MySQL database. I want to make it more universal. I'm not sure in myself. My class can connect any server if you specify the database name.

String url="jdbc:mysql://"+host+":"+port+"/"+dbname;

How can I connect without the specifying the database name(to use create database command for example)? Is it correct to use:

String url="jdbc:mysql://"+host+":"+port

Or does it use a different syntax?


Solution

  • Yes, you can use String url="jdbc:mysql://"+host+":"+port but during the execution of query you need to specify the database name as

    select * from dbname.table