0
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;

public class ConnectionManager {
    private static String URL  = "jdbc:google:mysql://your-project-id:your-instance-name/database";
    private static String DRIVER = "com.mysql.jdbc.GoogleDriver";
    private static String USERNAME = "root";
    private static String PASSWORD = "test";
    private static Connection CON = null;

    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
            try {
                CON = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            } catch (SQLException se) {
                //Handle errors for JDBC
                se.printStackTrace();
            }
        } catch (Exception ex) {
            //Handle errors for Class.forName
            ex.printStackTrace();
        }

        //return a connection object
        return CON;
    }

}

I created the above class to connect to my Cloud SQL instance in Android Studio but the above class is returning a NULL which in turn hurts the rest of my code:

conn = ConnectionManager.getConnection();
stmt = conn.createStatement();
String sql;
sql = "INSERT into messages (message) VALUES('Hello World')";
ResultSet rs = stmt.executeQuery(sql);

I'm not sure though why I cam getting this error:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference

Am I missing some imports or something?

9
  • 1
    What are you doing here? Class.forName(DRIVER); You don't use the return value of that method call... Are you getting any SQLExceptions or anything else during that method call? Commented Jun 19, 2015 at 16:25
  • Have you added the dependent libraries(jar) for that Refer Commented Jun 19, 2015 at 16:29
  • @refer Not sure how to do that... Commented Jun 19, 2015 at 16:33
  • Have a look here, this solves your problem: cloud.google.com/appengine/docs/java/cloud-sql/… Commented Jun 19, 2015 at 16:35
  • @NwDx I am very aware of that link but it didn't solve the problem. You can only connect to Cloud SQL if it is running in AppEngine apparently... Commented Jun 19, 2015 at 16:47

3 Answers 3

2
com.mysql.jdbc.GoogleDriver

Only works if your app is running in AppEngine,and remember to enable the connector in appengine. If not just use com.mysql.jdbc.Driver.

The guide is very simple: https://cloud.google.com/appengine/docs/java/cloud-sql/#enable_connector_j

Sign up to request clarification or add additional context in comments.

3 Comments

I see. I thought you could just access Cloud SQL like any other MySQL database out there. And 'com.mysql.jdbc.Driver' will only work on localhost no?
I think you can use it, i use a local database to develop and don't mix with production,but like it says in the guide// Alternatively, connect to a Google Cloud SQL instance using: // jdbc:mysql://ip-address-of-google-cloud-sql-instance:3306/guestbook?user=root , you just can't use the GoogleDriver because you don't have it, but you can have 2 instances 1 for develop 1 for production
I tried // jdbc:mysql://ip-address-of-google-cloud-sql-instance:3306/guestbook?user=root with com.mysql.jdbc.Driver and it did not work.
0

Found something interesting here: cloud.google.com/sql/docs/external

So you are able to connect with a normal MySQL Connector and without App Engine. You get the jar-file here: http://dev.mysql.com/downloads/connector/j/

To solve 1:

Authorize the IP or range of IPs from which you will be connecting

you need to get your ip. If you on a normal dsl you may get a dynamic ip, so you have to change this often in admin console of Cloud SQL.

1 Comment

Thanks. What sucks though is I have to whitelist my IP in the admin console though. I wouldn't be able to give the program to someone else without them also having to get their IP whitelisted.
0

This might be a cause of one of two things:

1) Missing permission. Try to add these in the Manifest,

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.access_wifi_state" />

2) Add "jtds-1.3.1.jar" file to app/lib forlder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.