Help! can't connect to mongodb using JDBC in maven project

Hello everyone,

I am a junior developer and just starting out so please bare with me.

I am working on an app using Maven and even though I managed to have a database locally using JDBC, I can’t connect to my mongo database online.

This is my code

package xxxxxxxxxx.dao.dbutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;

public class DBUtil {

	private static Connection conn;
	private DBUtil() {}
	private static final String URL = "jdbc:mongodb://cluster0.ktad2.mongodb.net:27017/test?ssl=true&authSource=admin";

	public static Connection openConnection() throws SQLException {

		java.util.Properties p = new java.util.Properties();
		p.setProperty("user", "xxxxxxx");
		p.setProperty("password", "xxxxxxxxxxxx");
		p.setProperty("database", "test");
		p.setProperty("loglevel", Level.SEVERE.getName());

		//Registers JDBC Driver
		try {
			Class.forName("com.mongodb.jdbc.MongoDriver");
		} catch (ClassNotFoundException e) {
			System.out.println("ERROR: Unable to load SQLServer JDBC Driver");
			e.printStackTrace();
		}
		System.out.println("MongoDB JDBC Driver has been registered...");


		//Connects to mongodb database

		System.out.println("Trying to get a connection to the database...");
		try {
			Connection conn = DriverManager.getConnection(URL, p);

		} catch (SQLException  e) {
			System.out.println("ERROR: Unable to establish a connection with the database!");
			e.printStackTrace();
		}
		return conn;
	}

I have tried everything I can think of and still getting a timeout error

as well as

MongoDB JDBC Driver has been registered...
Trying to get a connection to the database...
	at gr.aueb.cf.teachersapp.controller.SearchTeacherController.doGet(SearchTeacherController.java:39)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at gr.aueb.cf.teachersapp.controller.GRFilter.doFilter(GRFilter.java:25)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:829)
[2022-10-25 22:54:41.809] [SEVERE] [c-2] com.mongodb.jdbc.MongoConnection: Error in MongoConnection.testConnection(..) 
java.util.concurrent.TimeoutException
	at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:204)
	at com.mongodb.jdbc.MongoConnection.testConnection(MongoConnection.java:480)
	at com.mongodb.jdbc.MongoDriver.connect(MongoDriver.java:169)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
	at gr.aueb.cf.teachersapp.dao.dbutil.DBUtil.openConnection(DBUtil.java:35)
	at gr.aueb.cf.teachersapp.dao.TeacherDAOImpl.getTeachersByLastnane(TeacherDAOImpl.java:110)
	at gr.aueb.cf.teachersapp.service.TeacherServiceImpl.getTeacherByLastname(TeacherServiceImpl.java:86)
	at gr.aueb.cf.teachersapp.controller.SearchTeacherController.doGet(SearchTeacherController.java:39)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at gr.aueb.cf.teachersapp.controller.GRFilter.doFilter(GRFilter.java:25)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:829)
 
java.sql.SQLTimeoutException: Timeout. Can't connect.
	at com.mongodb.jdbc.MongoDriver.connect(MongoDriver.java:171)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
	at gr.aueb.cf.teachersapp.dao.dbutil.DBUtil.openConnection(DBUtil.java:35)
	at gr.aueb.cf.teachersapp.dao.TeacherDAOImpl.getTeachersByLastnane(TeacherDAOImpl.java:110)
	at gr.aueb.cf.teachersapp.service.TeacherServiceImpl.getTeacherByLastname(TeacherServiceImpl.java:86)
	at gr.aueb.cf.teachersapp.controller.SearchTeacherController.doGet(SearchTeacherController.java:39)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at gr.aueb.cf.teachersapp.controller.GRFilter.doFilter(GRFilter.java:25)
[2022-10-25 22:54:41.809] [SEVERE] [c-2] [stmt-1] com.mongodb.jdbc.MongoSQLStatement: Error in MongoSQLStatement.executeQuery(..) 
com.mongodb.MongoInterruptedException: Interrupted while waiting to connect
	at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:211)
	at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
	at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:155)
	at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:105)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:287)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:191)
	at com.mongodb.client.internal.MongoDatabaseImpl.executeCommand(MongoDatabaseImpl.java:194)
	at com.mongodb.client.internal.MongoDatabaseImpl.runCommand(MongoDatabaseImpl.java:163)
	at com.mongodb.client.internal.MongoDatabaseImpl.runCommand(MongoDatabaseImpl.java:158)
	at com.mongodb.jdbc.MongoSQLStatement.executeQuery(MongoSQLStatement.java:60)
	at com.mongodb.jdbc.MongoStatement.execute(MongoStatement.java:175)
	at com.mongodb.jdbc.MongoConnection$ConnValidation.call(MongoConnection.java:448)
ERROR: Unable to establish a connection with the database!
	at com.mongodb.jdbc.MongoConnection$ConnValidation.call(MongoConnection.java:1)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.InterruptedException
	at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(AbstractQueuedSynchronizer.java:1081)
	at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1369)
	at java.base/java.util.concurrent.CountDownLatch.await(CountDownLatch.java:278)

any help would be greatly appreciated!!

Contrary to localhost the name

is a cluster name. The connection method is different and you have to mongodb+srv:// as the connection method rather than simply mongodb://.

See https://www.mongodb.com/docs/manual/reference/connection-string/ for more details on connection string formats.