java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase - eclipse

I have mariadb 10.4 running on my system. The db connection is good, i have tested with db workbench with the root password.
On the other hand, I try to connect to it from my Java code, i have the exception below:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase
My java code is as below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class CountryDAO {
public void list() throws SQLException{
String databaseURL = "jdbc:mariadb://localhost:3306/mydatabase";
String user = "root";
String password = "root";
try
{
Connection connection = DriverManager.getConnection(databaseURL, user, password);
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
}
}
I am calling this code from a servlet.
I am using eclipse and i have added the mariadb-java-client-2.4.2.jar as external jar from build path. It should be working but it doesn't.
UPDATE: Firstly, I have tested my code in a new Eclipse Java project and it works like a charm. Secondly, i have tried to call the same method from a main method of another class, the db connection also work perfectly. So i guess my problem is because i call this connection from a Servlet. I don't know why it does so but i am kind of stuck. Do you have any suggestions?

You have to load the driver first.
Class.forName("org.mariadb.jdbc.Driver");

Related

MongoDB Jmeter with SSL File

So I'm following the guide over on BlazeMeter about how to load test MongoDB with Jmeter here
https://www.blazemeter.com/blog/mongodb-performance-testing-with-jmeter/
In my case I do have a connection string because our Mongo Deployment is using a replicaset. No biggie I just hardcoded it into the MongoClient section as I was having issues with passing it through as a variable.
But here is the deal. Our Mongo servers use a SSL file as a part of the authentication mechanism.
Is there a way I can just plug the SSL into a variable inside of jmeter and then have it be set properly in the database connection portion of the load test?
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
try {
MongoClient mongoClient = MongoClients.create("MY_CONNECTION_STRING");
MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
MongoCollection<Document> collection = database.getCollection(vars.get("collectionName"));
vars.putObject("collection", collection);
return "Connected to " + vars.get("collectionName");
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}```
As per MongoDB Driver Reference Connecting SSL
JVM system properties
A typical application will need to set several JVM system properties to ensure that the client is able to validate the SSL certificate presented by the server:
javax.net.ssl.trustStore: the path to a trust store containing the certificate of the signing authority
javax.net.ssl.trustStorePassword: the password to access this trust store
The aforementioned properties can be defined either in system.properties file (lives in "bin" folder of your JMeter installation) or passed to JMeter startup script via -D command-line arguments
Also make sure to have ?ssl=true directive in your MY_CONNECTION_STRING JMeter Variable

PostgreSQL database Versioning using Liquibase

I'm trying to use Liquibase version 3.6 to maintain database version changes. I'm able to execute database version changes when i need to execute single file changes.
I have using below code to execute version changes successfully, but my intention is to dynamically execute the change logs from a folder and not single file. I'm using only Java for all configuration of Liquibase
#Startup
#Singleton
#TransactionManagement(TransactionManagementType.BEAN)
public class InitializerBean {
#Resource(mappedName = "java:/M2M_RESOURCES")
private DataSource ds;
#PostConstruct
protected void bootstrap() {
ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(getClass().getClassLoader());
try (Connection connection = ds.getConnection()) {
JdbcConnection jdbcConnection = new JdbcConnection(connection);
Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
Liquibase liquiBase = new Liquibase("data/liquibase/", resourceAccessor, db);
liquiBase.update("Development");
} catch (SQLException | LiquibaseException e) {
}
}
}
When the first parameter of Liquibase class is single file , liquibase is able to execute changes but when I intent to execute all file of single folder is not able to track and execute changes.
I'm using JDK 1.8 and file in data/liquibase/ is dbChangelog.sql and dbChangelog_2.sq. This all code is deployed on Wildfly 10 as part of ear archive

Connect to MongoDb using X509 certificate

I am trying to connect to MongoDB using mongoX509. I am using mongo java driver 3.3.0 jar. In api reference I can see MongoCredential to pass subject name and authenticate but i am not able to import this in my code. When I decompile the jar I am not able to see MongoCredential as well.
Am I missing any dependencies. Is there any easy way to connect to Mongodb without using MongoCredential? The details i have is server, port and certificate subject name?
I can post the code I am trying if anyone wants to take a look at it as well
Thanks in Advance
Code - I am using SoapUI to run this code
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
try{
def subjectName="CN=xx,OU=xx,O=xx,C=US,ST=CA,L=xx"
MongoCredential credential = MongoCredential.createMongoX509Credential(subjectName);
def URI = "mongodb://server1:27017,server2:27017,server3:27017/<<database>>?replicaSet=<<XYZ>>&authMechanism=MONGODB-X509&ssl=true"
MongoClientURI uri = new MongoClientURI(URI)
MongoClient client = new MongoClient(uri, Arrays.asList(credential));
DB database = client.getDB(<<database>>);
collection = database.isAuthenticated();
log.info collection
}
catch (Exception e){
log.info e
}
The issue was because of incorrect jar that I was using. I got the correct version and it worked.

How to include driver class and/or class path to connect to a SQL Server 2000 database in Jdeveloper?

I want to connect to a database in Jdeveloper 11g but there seems to be a problem with my driver for SQL Server 2000. I have downloaded the appropriate driver (Sqljdbc4.jar) but I do not know how to include or use it in my code.
import java.sql.*;
class TestConnection
{
public static void main (String[] args)
{
try
{
// Step 1: Load the JDBC ODBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Step 2: Establish the connection to the database
String url = "jdbc:sqlserver://10.1.73.180\\SQL2000;" +
"databaseName=reportgen;user=sa;password=*****;";
Connection conn = DriverManager.getConnection(url);
System.out.println("Connected.");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
and this shows when I try to run my TestConnection.java class:
Got an exception!
com.microsoft.sqlserver.jdbc.SQLServerDriver
Process exited with exit code 0.
Project properties->libraries and class path->add JAR.
You might also need to add this to the embedded WebLogic instance.
You can also use tools->manage libraries to add a library and then add that library to your project.

GWT JDBC LDAP connection fails

I am trying to connect my GWT application to an ldap server using jdbc, but could not make it work so far.
Here is a code sample of my attempt to connect to it:
String ldapConnectString = "jdbc:ldap://SERVERIP:389/dc=SERVERNAME,dc=office,dc=COMPANY,dc=com?SEARCH_SCOPE:=subTreeScope";
java.sql.Connection con;
try {
con = DriverManager.getConnection(ldapConnectString,"cn=USERNAME","PASSWORD");
} catch (SQLException e) {
System.out.println("An error has ocurred!!! Connection failed");
e.printStackTrace();
}
The example I used to write this is: http://myvd.sourceforge.net/bridge.html
When I run the application I get following error message:
java.sql.SQLException: No suitable driver found for jdbc:ldap://SERVERIP:389/dc=SERVERNAME,dc=office,dc=COMPANY,dc=com?SEARCH_SCOPE:=subTreeScope
I would be thankful for any help
Edit:
The code sample I provided is running on server side accessed by RPC. I included 2 jar files in my lib/ directory downloaded from here: http://sourceforge.net/projects/myvd/files/jdbc%20ldap%20bridge/jdbc%20ldap%20bridge%202.1/jdbc-ldap-2.1.zip/download
You generally need to register the JDBC driver before you can connect to the backend.
Try something like
DriverManager.registerDriver(new com.octetstring.jdbcLdap.sql.JdbcLdapDriver());
before setting up the connection.
More general information on ways of registering drivers.