Skip to content

Commit

Permalink
[misc] handling connection error when no database is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Aug 22, 2017
1 parent 39885f4 commit 562aeb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Expand Up @@ -621,7 +621,7 @@ private void additionalData() throws IOException, SQLException {
sendPipelineCheckMaster();
readPipelineCheckMaster();

if (options.createDatabaseIfNotExist) {
if (options.createDatabaseIfNotExist && !database.isEmpty()) {
// Try to create the database if it does not exist
String quotedDb = MariaDbConnection.quoteIdentifier(this.database);
sendCreateDatabaseIfNotExist(quotedDb);
Expand Down Expand Up @@ -845,7 +845,7 @@ private long initializeClientCapabilities(long serverCapabilities) {

// If a database is given, but createDatabaseIfNotExist is not defined or is false,
// then just try to connect to the given database
if (database != null && !options.createDatabaseIfNotExist) {
if (!database.isEmpty() && !options.createDatabaseIfNotExist) {
capabilities |= MariaDbServerCapabilities.CONNECT_WITH_DB;
}
return capabilities;
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/org/mariadb/jdbc/BaseTest.java
Expand Up @@ -205,7 +205,8 @@ public static void beforeClassBaseTest() throws SQLException {
}

private static void setUri() {
connU = "jdbc:mariadb://" + ((hostname == null) ? "localhost" : hostname) + ":" + port + "/" + database;
connU = "jdbc:mariadb://" + ((hostname == null) ? "localhost" : hostname) + ":" + port + "/"
+ ((database == null) ? "" : database);
connUri = connU + "?user=" + username
+ (password != null && !"".equals(password) ? "&password=" + password : "")
+ (parameters != null ? "&" + parameters : "");
Expand Down Expand Up @@ -511,15 +512,17 @@ protected Connection setConnection(String parameters) throws SQLException {
}

protected Connection setConnection(String additionnallParameters, String database) throws SQLException {
String connU = "jdbc:mariadb://" + ((hostname == null) ? "localhost" : hostname) + ":" + port + "/" + database;
String connU = "jdbc:mariadb://" + ((hostname == null) ? "localhost" : hostname) + ":" + port + "/"
+ ((database == null) ? "" : database);
String connUri = connU + "?user=" + username
+ (password != null && !"".equals(password) ? "&password=" + password : "")
+ (parameters != null ? "&" + parameters : "");
return openConnection(connUri + additionnallParameters, null);
}

protected Connection setDnsConnection(String parameters) throws SQLException {
String connU = "jdbc:mariadb://mariadb.example.com:" + port + "/" + database;
String connU = "jdbc:mariadb://mariadb.example.com:" + port + "/"
+ ((database == null) ? "" : database);
String connUri = connU + "?user=" + username
+ (password != null && !"".equals(password) ? "&password=" + password : "")
+ (parameters != null ? "&" + parameters : "");
Expand Down

0 comments on commit 562aeb2

Please sign in to comment.