GoogleCloudPlatform / cloud-sql-jdbc-socket-factory

A collection of Java libraries for connecting securely to Cloud SQL
Apache License 2.0
234 stars 119 forks source link

No Suitable Driver Found For JDBC Connection #39

Closed asktushar88 closed 7 years ago

asktushar88 commented 7 years ago

Hello,

I am trying to test the above code, Without doing much changes, i ran the maven project. I am getting error which says -

Exception in thread "main" java.sql.SQLException: No suitable driver found for j
dbc:mysql://google/cdpDev?cloudSqlInstance=cdp-dev-201706-01:us-central1:cdp-dev
-cl-mysql-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at com.google.cloud.sql.mysql.example.ListTables.main(ListTables.java:40
)

Please help to fix this issue.

Tried to do everything, but no help. Hope someone can help me out here.

Thanks

vadimberezniker commented 7 years ago

Please share the maven command you used to run the sample. This message would indicate that the classpath is not setup properly.

asktushar88 commented 7 years ago

Hello, Thanks for your prompt reply.

I did mv clean package for the Build. And then below command to execute - java -cp D:\GCP\cloud-sql-jdbc-socket-factory-master\cloud-sql-jdbc-socket-factory-master\examples\mysql\compute-engine\target\mysql-socket-factory-compute-engine-1.0.0-SNAPSHOT.jar com.google.cloud.sql.mysql.example.ListTables

Thanks in advance. Please let me know if i am doing anything wrong here.

vadimberezniker commented 7 years ago

That will only include the library itself in the classpath and none of its dependencies. Use maven to setup the classpath, run mvn compile exec:java which will run the application with the correct classpath.

toannv53 commented 7 years ago

Dear Laixer, I'm trying to implement google cloud SQL, but I am getting the same error.

java.sql.SQLException: No suitable driver found for jdbc:mysql://google/my_table?useSSL=false&cloudSqlInstance=Instance_connection_name&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=** at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:270) at com.geekteam.kanayomi.HelloAppEngine.init(HelloAppEngine.java:113) at javax.servlet.GenericServlet.init(GenericServlet.java:244) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:643) at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:499) at org.eclipse.jetty.servlet.ServletHolder.ensureInstance(ServletHolder.java:791) at org.eclipse.jetty.servlet.ServletHolder.prepare(ServletHolder.java:776) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:579) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) at com.google.appengine.tools.development.jetty9.DevAppEngineWebAppContext.doScope(DevAppEngineWebAppContext.java:94) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) at com.google.appengine.tools.development.jetty9.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:597) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) at org.eclipse.jetty.server.Server.handle(Server.java:534) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:283) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:108) at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) at java.lang.Thread.run(Thread.java:748)

bellow is my Pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>0.1.0-SNAPSHOT</version>

<groupId>com.***</groupId>
<artifactId>***</artifactId>

<properties>
    <appengine.maven.plugin.version>1.3.1</appengine.maven.plugin.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
    <INSTANCE_CONNECTION_NAME>Instance connection name</INSTANCE_CONNECTION_NAME>
    <user>root</user>
    <password>******</password>
    <database></database>

</properties>

<prerequisites>
    <maven>3.3.9</maven>
</prerequisites>

<dependencies>
    <!-- Compile/runtime dependencies -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test Dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>1.9.51</version>
    </dependency>
    <dependency> <!-- Only used locally -->
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.42</version>  <!-- v5.x.x is for production, v6.x.x EAP X DevAPI -->
    </dependency>
    <dependency>
        <groupId>com.google.cloud.sql</groupId>
        <artifactId>mysql-socket-factory</artifactId> <!-- mysql-socket-factory-connector-j-6 if using 6.x.x -->
        <version>1.0.4</version>
    </dependency>
</dependencies>

<build>
    <!-- for hot reload of the web application -->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>display-dependency-updates</goal>
                        <goal>display-plugin-updates</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>${appengine.maven.plugin.version}</version>
        </plugin>
    </plugins>
</build>

toannv53 commented 7 years ago

use mysql-connector-java version 6.0.6 will fix it

mysql mysql-connector-java 6.0.6
vadimberezniker commented 7 years ago

The error "No suitable driver found" is a general JDBC error thrown when a driver is not registered and is not related to this library. Please refer to: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager

Vsingamaneni commented 6 years ago

Can someone tell me how did you fix this error?

ghost commented 5 years ago

Hi, Did somebody solved this problem ? i tried the both answers from laixer and toannv53 but not working.

Vsingamaneni commented 5 years ago

I have faced the same issue the first time i tried to deploy my application in google cloud.

I have changed the connection string to below. The below changes worked for me.

in your appengine-web.xml, define the following.

<?xml version="1.0" encoding="utf-8"?>

java8 true YOUR_APP_NAME xxx true xxx

In your pom,

GET_FROM_YOUR_SQL_DB root password xyz

In the spring connection, use the data source to load this value.

Good luck.

Thanks, Vamsi

On Mon, Apr 22, 2019 at 2:49 PM jihad notifications@github.com wrote:

Hi, Did somebody solved this problem ? i tried the both answers from laixer and toannv53 but not working.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/39#issuecomment-485529168, or mute the thread https://github.com/notifications/unsubscribe-auth/AJH6JDQOYHXQWV7D5AL6S4LPRYJDTANCNFSM4DS422HQ .

-- Programmer Analyst Walmart

Walmart David Glass Technology Center 805 Moberly Ln Bentonville, AR 72716 Walmart Email: vamsi.singamaneni0@walmart.com vamsi.singamaneni@walmart.com Linked In : https://www.linkedin.com/in/vsingamaneni https://www.linkedin.com/in/vsingamaneni Cell: +1 (617) 378-1238

ghost commented 5 years ago

Thanks Vsingamaneni, But I think I am changing the project to Gradle. At least it worked with the old configuration.