NullPointerException in "DriverManager.getConnection" method

Go To StackoverFlow.com

0

I have a problem establishing an connection to my MySql database via Java/Android. I have a database file (MyDatabase.db) on a Windows7 computer in my network. When I'm developing from another Windows7 computer (the file is accessible via the Windows Explorer and I can make changes to the database via SQLDatabaseExplorer) out of Eclipse the following Code works, but when installing my Application on my Galaxy Tab the "DriverManager.getConnection()" returns null.

       try {
           String url = "http://192.168.178.21/Users/test/userdata/Database/MyDatabase.db";
           Class.forName ("com.mysql.jdbc.Driver");
           Connection connection = DriverManager.getConnection (url);
           System.out.println ("Database connection established");
       } catch (SQLException e) {
           Log.d("SQLException", "e.toString()");
       }

The SQLException logged in LogCat is:

java.sql.SQLException: path to '//192.168.178.21/Users/test/userdata/Database/MyDatabase.db': '/192.168.178.21' does not exist 

I guess my problem lies in the url String...? But I did not figure out how to change it, so that the connection can be established.

Thanks for any help, Tim

EDIT: Thanks for your help so far! I have written the question yesterday out of my mind, without looking onto my code... I'm sorry for that, because I have mixed up a lot of things... It is not a MySql-database but a sqlite-database... But I think that doesn't change a lot in coding. I'm using an jdbc sqlite driver. When starting the lines below in an Java-Eclipse Project everything works fine and the connection can be established. But on my Device I still got the Nullpointer...

Yesterday I have changed my code so that it should fit your advices. But the problem still resists... To be sure that it does not have to do with some rights or network settings I have copied the DB-File onto my Androiddevice and tried to connect to it directly with the following lines of code:

try {
    Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

Connection conn = DriverManager.getConnection("jdbc:sqlite://mnt/sdcard/MyVideos34.db");

if (conn == null) {
    Log.d("ConnectionError", "Connection is null");
return;
}

But also here getConnection throws a NullPointer and I don't know why... Did somebody have a assumption why the connection can be established out of Eclipse and fails on my Androiddevice? May I could have a wrong driver, that does not work from the device, but from Eclipse...?

Thanks in advance!

2012-04-04 06:51
by user1162729
possible duplicate of How to syncronize Data with a ServerDatabase?Snicolas 2012-04-04 06:55
use getConnection(url,username,password) instead of getConnection (url);ρяσѕρєя K 2012-04-04 06:57


3

The url format for the MYSQL conenction string is

jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

jdbc:mysql://[host:port],[host:port].../[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

Here is a sample connection URL:

jdbc:mysql://localhost:3306/sakila?profileSQL=true

please change accordingly

2012-04-04 06:54
by Akash Yadav


0

JDBC urls have the form : jdbc:mysql:/// but look at the duplicate code. You probably don't want to connect directly to a database from a mobile like this but will prefer a web service wrapper to do it.

2012-04-04 06:55
by Snicolas


0

try {
       String url = "jdbc:mysql://localhost:3306/MyDatabase?user=root&password=root";
       Class.forName ("com.mysql.jdbc.Driver").newInstance();
       Connection connection = DriverManager.getConnection (url);
       System.out.println ("Database connection established");
    } catch (SQLException e) {
       System.out.println("SQLException" + e.toString());
    } catch (ClassNotFoundException e) {
       e.printStackTrace();
    } catch (InstantiationException e) {
       e.printStackTrace();
    } catch (IllegalAccessException e) {
       e.printStackTrace();
    }
2012-04-04 07:31
by Chitresh
Ads