Android 中的 jdbc 问题 - 无法连接到数据库
我是 jdbc 和 android 的新手。如果这个问题看起来很愚蠢,请原谅我。
我正在尝试从应用程序连接到 MySql 数据库。
我浏览了 jdbc 教程并编写了以下代码:
public static void connectToServer ()
{
Connection conn = null;
try
{
//Connect to the database
String userName = "********";
String password = "********";
String url = "jdbc:mysql://my.domain.name/myDBname";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
Log.e(tag,"Database connection established");
// Query the database
Statement s = conn.createStatement ();
String query = "INSERT INTO myTableName (A,B,C)" +
"VALUES ('a','b','c')");
Log.e(tag,query);
s.executeQuery (query);
s.close ();
}
catch (Exception e)
{
Log.e(tag,"Database Connection Failed");
Log.e(tag,e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
Log.e(tag,"Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
我的清单代码:
<代码> <使用权限 android:name="android.permission.ACCESS_FINE_LOCATION" />
<使用权限 android:name="android.permission.ACCESS_COARSE_LOCATION" />
日志猫是:
<代码> ....
数据库连接终止//我的输出
com.mysql.jdbc.Driver // 异常 e.getMessage() 的输出
我做错了什么?
从 java 应用程序和 Android 应用程序连接有区别吗?
编辑:添加清单权限
I am new to jdbc and android. So pardon me if the question seems silly.
I am trying to connect to a MySql database from an app.
I went through the jdbc tutorial and wrote the following code:
public static void connectToServer ()
{
Connection conn = null;
try
{
//Connect to the database
String userName = "********";
String password = "********";
String url = "jdbc:mysql://my.domain.name/myDBname";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
Log.e(tag,"Database connection established");
// Query the database
Statement s = conn.createStatement ();
String query = "INSERT INTO myTableName (A,B,C)" +
"VALUES ('a','b','c')");
Log.e(tag,query);
s.executeQuery (query);
s.close ();
}
catch (Exception e)
{
Log.e(tag,"Database Connection Failed");
Log.e(tag,e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
Log.e(tag,"Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
My manifest code:
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
The logcat is:
....
Database connection terminated //my output
com.mysql.jdbc.Driver // output from exception e.getMessage()
What am I doing wrong?
Is there a difference between connecting from a java application and android app?
EDIT : Added manifest permissions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您在清单中请求互联网权限。
编辑:为此,请将
添加到您的清单中。刚刚找到此链接。不确定这是否可以轻松完成。
另外值得研究的是 CouchDB。它基于 REST,因此您不必围绕 MySQL DB 构建 php 服务 - 它纯粹是 REST。
Be sure you are requesting internet permissions in your manifest.
EDIT: To do this, add
<uses-permission android:name="android.permission.INTERNET" />
to your manifest.Just found this link. Not sure if this can be done easily.
Also something else to look into is CouchDB. Its REST based, so you wouldn't have to build a php service around the MySQL DB - its purely REST.
我知道这对一些人有用。确保使用 JDBC 连接器 mysql-connector-java-3.0.17-ga-bin.jar。谷歌一下。
I know that this has worked for some. Make sure you use the JDBC connector mysql-connector-java-3.0.17-ga-bin.jar. Google for it.
为什么不花一些时间学习 android.database.sqlite 呢?恕我直言,从长远来看这是值得的。
祝你好运。
Why not spend some time learning android.database.sqlite instead? IMHO, it'll be worth it in the long run.
Good luck.