I'm trying to connect my Java Maven project (which runs on a Jetty server) to a Google Cloud MySQL Database with the following code:
private static final String CREDENTIALS_STRING = "jdbc::mysql://google/weatherplanning?cloudSqlInstance=csci310-project-2:us-central1:myinstance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=xxxx&password=xxxx";
try {
System.out.println("Connecting to database...");
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(CREDENTIALS_STRING);
} catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've included both of these dependencies in my pom.xml:
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-8</artifactId>
<version>1.0.15</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
I'm getting
java.sql.SQLException: No suitable driver found for jdbc::mysql://google...
and I don't know what to do. I assume the dependencies should handle adding the JDBC driver to my classpath.
Your problem looks like you have a typo in your JDBC url. Your URL should start with jdbc:mysql instead of jdbc::mysql.
Additionally, you should always use a connection pool (like Hikari). Connection pools have many benefits, including better error handling, exponential backoff, and reduced latency.
You can view Hikari being used with the Cloud SQL JDBC socket factory in the context of a web application in this sample. Full instructions for deploying it are in the sample's README.
Related
I'm trying to install and connect to a local Informix 14 for testing on Windows 10. My maven POM has:
<dependency>
<groupId>com.ibm.informix</groupId>
<artifactId>informix-jdbc-complete</artifactId>
<version>4.50.4.1</version>
<scope>test</scope>
</dependency>
I'm trying direct connection
database.url = jdbc:informix-direct://sysmaster
database.driver=com.informix.jdbc.IfxDirectConnection
But I'm getting class not found.
Any ideas?
You shouldn't use 'jdbc:informix-direct:'. That was reserved for UDR written in Java.
Use "jdbc:informix-sqli", something like:
Class.forName("com.informix.jdbc.IfxDriver");
conn = DriverManager.getConnection("jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1410;user=informix;password=mypassword;");
Since yesterday I have been trying to figure out how to fix this SQLException, and at this point I'm empty. I have checked dozens of sites, the whole documentation for Microsoft and Azure on how to connect with databases, and I keep getting the same error.
I've added a CLASSPATH like CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 8.2 for SQL Server\sqljdbc_8.2\enu\mssql-jdbc-8.2.2.jre11.jar as informed on Using the JDBC Driver documentation. I've downloaded the JDBC driver from Microsoft themselves and dumped the files on a folder in C:\Program Files\Microsoft JDBC DRIVER 8.4 for SQL Server as their documentation suggests. My maven and java are working fine, and I have an example class that I've got from SQLExamples by Microsoft themselves, this one:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// As informed on:
// https://www.microsoft.com/en-us/sql-server/developer-get-started/java/windows/step/2.html
public class App {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver:[AzureAddressHere];"
+ "database=[DatabaseHere];"
+ "user=[UserHere];"
+ "password=[PasswordHere]";
try {
// Load SQL Server JDBC driver and establish connection.
System.out.print("Connecting to SQL Server ... ");
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
System.out.println("Done.");
}
} catch (SQLException e) {
System.out.println();
e.printStackTrace();
}
}
}
I've also added the references to the jar file to the pom.xml dependencies list:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre11</version>
</dependency>
I've also found a mention in a website that the jar file sits in Maven's library folder, so I've copied the jar file to the lib folder as well.
Yet, whenever I try to run the app, I get the java.sql.SQLException: No suitable driver found for [AzureInformation].
Any ideas on what I could be missing here?
Turns out that the issue was the lack of two // in the address. facepalm
So, I just set this line to: String connectionUrl = "jdbc:sqlserver://[AzureAddressHere];" and it finally connected.
To be fair to myself here, this error is so completely unclear and unhelpful.
I'm new to JSP, so I'm playing around with it a bit. I've created a Maven project in Intellij and imported the dependencies that I need in my pom.xml, namely mysql-connector and servlet-api:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
<scope>compile</scope>
</dependency>
</dependencies>
I have a JSP file that accesses the local MySQL database using the corresponding Driver.
<%
String url = "jdbc:mysql://localhost:80/DemoJSP";
String username = "root";
String password = "";
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
%>
However, when I run the Tomcat server, I get HTTP Status 500. The cause of this is the line Class.forName("..."), so the thrown exception is java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. I have tried thousands of Maven reimports, but nothing helps. Is there anything that I'm missing?
P.S.: similar questions like How to use Maven to Create JSP + Servlet + TOMCAT + MySQL or Where do I have to place the JDBC driver for Tomcat's connection pool? do not solve my problem.
Try in url for localhost port 3306 not 80.
And if you get an error whit time zone, use this string :
String url = "jdbc:mysql://127.0.0.1:3306/DemoJSP";
String timezone = "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String username = "root";
String password = "";
Connection connection = DriverManager.getConnection(url + timezone, username, password);
When I add this to the pom.xml:
<!-- https://mvnrepository.com/artifact/us.fatehi/schemacrawler-mariadb -->
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler-mariadb</artifactId>
<version>14.08.06</version>
</dependency>
Then I get an error:
java.util.ServiceConfigurationError: schemacrawler.tools.databaseconnector.DatabaseConnector: Provider schemacrawler.server.mariadb.MariaDBDatabaseConnector could not be instantiated
..
Caused by: java.lang.NoSuchMethodError: schemacrawler.tools.databaseconnector.DatabaseConnector.<init>(Lschemacrawler/tools/databaseconnector/DatabaseServerType;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
I am trying to connect to an Oracle database. This works if I omit MariaDb from the pom.
I am using a higher version of SchemaCrawler:
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler</artifactId>
<version>14.21.02</version>
</dependency>
<!-- https://mvnrepository.com/artifact/us.fatehi/schemacrawler-oracle -->
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler-oracle</artifactId>
<version>14.21.02</version>
</dependency>
I would like to have the MariaDB in the pom.xml and still be able to read Oracle with SchemaCrawler. The error occurs after connecting to the database, in the last line of the following code:
Connection dbConnection = DatabaseBroker.getDbConnection(
eventName,
cbDatabase.getValue(),
tConnectionString.getValue(),
tUsername.getValue(),
tPassword.getValue()
);
//Schema schema = SchemaCrawler.getSchema(dbConnection, SchemaInfoLevel.detailed(), new SchemaCrawlerOptions());
//SchemaCrawler sc = new SchemaCrawler(dbConnection, null);
try
{
Catalog catalog = SchemaCrawlerUtility.getCatalog(dbConnection, null);
You are using incompatible versions of the main SchemaCrawler library and a SchemaCrawler database plugin. You do not need a plugin for MariaDB if you are connecting to Oracle. In fact, SchemaCrawler will work with most databases even without a SchemaCrawler database plugin on the classpath.
I've searched a lot and spent many time trying register JDBC driver.
First, I copied my ojdbc7.jar file (downloaded from Oracle) into directory shown below:
Driver File(s): /Users/Kamil/glassfish4/jdk7/jre/lib/ext/ojdbc7.jar
Driver Class: oracle.jdbc.OracleDriver
// this is copied from Services/Databases/Drivers/ojdbc
Then, I tried following code:
try {
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(myDriver);
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
... and this one:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
... and with this line instead:
Class.forName("oracle.jdbc.driver.OracleDriver");
I always get ClassNotFoundException :(
Here is code I try to run:
Connection DBconn;
String USER = "root";
String PASS = "root";
System.out.println("Connecting to database...");
DBconn = DriverManager.getConnection("mysql://localhost:3306/RestToolDatabase", USER, PASS);
System.out.println("Creating statement...");
Statement stmt = DBconn.createStatement();
String sql;
sql = "select surname, id, age\n"
+ "from customers \n"
+ "where name = \"maria\" \n"
+ "order by id;";
ResultSet rs = stmt.executeQuery(sql);
I've also read about setting the classpath as described here:
Right-click your Project.
Select Properties.
On the left-hand side click Libraries.
Under Compile tab - click Add Jar/Folder button.
but there is no "Properties/Libraries" option in NetBeans...
I use Maven and there is following dependency added by some library:
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
<type>pom</type>
</dependency>
Maybe there is some workaround, or other way to add it automatically? It SHOULD be simple but I'm unexperienced and wasted many time on this. Please help.
EDIT: Thank you for replies, yes, I use MySQL Server at localhost:3306 [root]. I have MySQL JDBC connector installed here:
/Applications/NetBeans/NetBeans 8.0.app/Contents/Resources/NetBeans/ide/modules/ext/mysql-connector-java-5.1.23-bin.jar
When I go to "Services" --> "Drivers" --> "MySQL (Connector/J driver)" there is Driver Class path exactly as you suggested, so I use Class.forName("com.mysql.jdbc.Driver") now.
I right-clicked on "MySQL (Connector/J driver)" driver and went to "Connect Using..." --> "localhost, port 3306, user, password". And it is connected now, I see new connection. But still I get ClassNotFoundException.
EDIT 2 - this solution worked for me:
I added following to dependencies in pom.xml:
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
... and builded application; the driver was downloaded and installed. As simple as that... I spent many time on this... It works - yeah! :)
You are using an Oracle JDBC driver on a MySQL database, you should use
Class.forName("com.mysql.jdbc.Driver");
Edit: Thanks to the comments from #duffymo and #Mark-Rotteveel who noticed that the URL of the connection is also wrong, the correct connection is:
Connection DBconn;
String USER = "root";
String PASS = "root";
DBconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/RestToolDatabase?" + "user="+USER+"&password="+PASS);
Setting the classpath :
Download the JAR JDBC Connector/Driver
Copy file in subfolder of project (ex: libs) (not 'src')
Project->Properties->Libraries->Add Jar/Folder->Select file
And packaging dependent librairies with relative path in 'dist' :
Project->Build->Packaging->Check "Copy Dependent Librairies"
I don't think the JDBC JAR belongs in that directory. The CNF exception backs me up.
I would recommend putting that JAR in your project WEB-INF/lib, repackaging the WAR, and trying again.
This line is correct:
Class.forName("oracle.jdbc.driver.OracleDriver");
The full class name has to match that in the JAR. This is the only one that does.
You have to use the driver that matches your database: Oracle for Oracle, MySQL for MySQL. What database are you using?
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
This worked for me. Since there's no Property and Library in Netbeans apache. Good luck.