Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC connection without database definition

Tags:

java

mysql

jdbc

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;

Can someone tell me, 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 it uses other syntax? Thanks in advance!

like image 633
Krisztián Dudás Avatar asked Feb 03 '14 14:02

Krisztián Dudás


1 Answers

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
like image 172
Kick Avatar answered Sep 20 '22 21:09

Kick