简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM