Daniel B
Daniel B

Reputation: 3

Using Java with a Google Cloud SQL service

I am trying to use Java to comunicate with a Google cloud SQL database, i have researched how to use jdbc to connect to MYSQL databases, and it has worked before, but for some reason it dosnt work with Google cloude SQL. Here is the code i am using to connect to the database

package DatabaseHelpers;

import java.sql.*;

public abstract class DatabaseHelper
{
    // Tutorial: http://mrbool.com/how-to-connect-with-mysql-database-using-java/25440
    private static String dbUrl = "jdbc:mysql://173.194.253.75:3306/snippet";
    //private static String dbUrl = "jdbc:google:mysql://rich-meridian-626:snippet/snippet?user=root";
    private static String dbName = "snippet";
    private static String dbUserName = "root";
    private static String dbPassword = "";

    protected Connection con = null;
    protected Statement stmt = null;

    /**
     * Opens a connection to the database in question
     */
    public DatabaseHelper() throws SQLException
    {
        con = DriverManager.getConnection(dbUrl);
        con.setAutoCommit(true);
        System.out.println(con.getAutoCommit());
    }

    /**
     * Clears the database
     * 
     * @throws SQLException
     */
    public abstract void clear() throws SQLException;

    /**
     * Closes the connection to the database
     * 
     * @throws SQLException
     */
    public void close() throws SQLException
    {
        con.close();
    }
}    

And this is the error message it gives me

Exception in thread "main" java.sql.SQLException: invalid database address: jdbc:mysql://173.194.253.75:3306/snippet
    at org.sqlite.JDBC.createConnection(JDBC.java:110)
    at org.sqlite.JDBC.connect(JDBC.java:87)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DatabaseHelpers.DatabaseHelper.<init>(DatabaseHelper.java:27)
    at DatabaseHelpers.FeedDatabaseHelper.<init>(FeedDatabaseHelper.java:11)
    at ArticalCollector.main(ArticalCollector.java:24)

Upvotes: 0

Views: 176

Answers (1)

Tony Tseng
Tony Tseng

Reputation: 479

The stack trace indicates that the sqlite JDBC driver was being used:

 at org.sqlite.JDBC.createConnection(JDBC.java:110)
 at org.sqlite.JDBC.connect(JDBC.java:87)

You'd want to use the MySQL JDBC driver instead.

Upvotes: 1

Related Questions