TL;DR : you need to hard check your URL or give us which driver and version you use, where you downloaded it from, how you use it. because I tried this and have absolutely no problem connecting if credentials has no typo.
I have this working file I extracted and adapted from one of test files in the source of wise-coders/mongodb-jdbc-driver: MongoDB JDBC Driver | DbSchema MongoDB Designer (github.com)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class SimpleTest {
private static Connection con;
private static final String urlWithAuth = "jdbc:mongodb://corpus:passpass@localhost:27017/corpus_test?authSource=corpus_test";
public static void main(String args[]) throws ClassNotFoundException, SQLException {
Class.forName("com.wisecoders.dbschema.mongodb.JdbcDriver");
Connection connection = DriverManager.getConnection( urlWithAuth, null, null);
Statement stmt=connection.createStatement();
//stmt.execute("use corpus_test;"); // use this or embed after hostname "localhost:27017/corpus_test"
System.out.println( "\n" );
stmt.executeQuery("corpus_test.listCollectionNames()");
stmt.close();
}
}
- save this code into
SimpleTest.java
- download driver from http://www.dbschema.com/jdbc-drivers/MongoDbJdbcDriver.zip and open it into
MongoDbJdbcDriver
folder with the file - compile with
javac SimpleTest.java
- run with
java -cp MongoDbJdbcDriver/mongojdbc4.1.jar;. SimpleTest
after testing many wrong credentials, as soon as I have it correct, there was only 1 error (of type 13) for trying to use admin
without permission. and it was just as easy as adding corpus_test
after host:port portion, or use stmt.execute("use corpus_test;");
before any more queries.
I have also tried DbSchema
and got auth error a few times, but it fixed itself somehow after a few connection attempts. maybe the driver it downloaded was not yet fully downloaded. I don’t know. or the connection pooling failing due to my low connection limit of 10. but at the end, there was not a single leftover to get the error you experienced.
so please this time be more clear and explaining. Examine your URL carefuly and if it still won’t solve then give use more. Which driver and which version is it. Where you get it and how you use it. how you import driver and how you compile your source.