本文介绍了NullPointerException异常的" DriverManager.getConnection就"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立通过Java / Android的我的MySQL数据库的连接有问题。我有一个Windows7电脑上的数据库文件(MyDatabase.db)我的网络。当我从另一个Windows7电脑开发(该文件是通过Windows资源管理器访问,我可以更改通过SQLDatabaseExplorer数据库中)出来的Eclipse以下code的作品,但在我的Galaxy Tab安装我的应用程序时, 的DriverManager.getConnection()将返回null。

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()");
       }

登录LogCat中SQLException的是:

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

我想我的问题就出在URL字符串...?但我也没弄明白怎么改,这样可以建立连接。

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

编辑:
感谢您的帮助迄今!昨天我写这个问题在我心里,没有看到我的code ......我对此感到抱歉,因为我已经混了很多事情......这不是一个MySQL数据库但sqlite的数据库...但我认为这并不编码发生很大的变化。我使用的是JDBC驱动程序源码。当开始低于行的Java的Eclipse项目的一切工作正常,可以建立连接。但在我的设备我还是得到了空指针...

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...

昨天我已经改变了我的code,以便它应该适合你的建议。但问题仍然抗拒......是的,它不具有某些权利或我抄DB-文件到我Androiddevice并试图网络设置做直接与code以下线路连接到它

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;
}

但这里也的getConnection抛出一个空指针,我不知道为什么...没有人有一个假设,为什么可以连接的Eclipse建立了与失败对我的Andr​​oiddevice?我可能可以有一个错误的驱动程序,不从设备工作,但在Eclipse ...?

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...?

在此先感谢!

推荐答案

对于MySQL conenction字符串URL格式为

The url format for the MYSQL conenction string is

JDBC的:mysql:// [主持人] [,... failoverhost] [:端口] / [数据库]»
[= propertyValue1] [&放大器; propertyName2] [propertyName1?] [= propertyValue2] ...

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

如果未指定主机名,则默认为127.0.0.1。如果未指定端口,默认为3306,为MySQL服务器的默认端口号。
JDBC的:mysql:// [主机:端口],[主机:端口] ... / [数据库]»
[= propertyValue1] [&放大器; propertyName2] [propertyName1?] [= 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]...

下面是一个简单的连接网址:
JDBC的:mysql://本地主机:3306 / sakila的profileSQL =真

Here is a sample connection URL:jdbc:mysql://localhost:3306/sakila?profileSQL=true

请相应地更改

这篇关于NullPointerException异常的" DriverManager.getConnection就"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 03:15