一样的程序,在Eclipse建一个java工程,就可以写在阿里云的mysql数据库,
移到Android工程,就报错了。是不是手机的防火墙什么的把链接阻挡了?
错误如下
data error 2
jdbc:mysql://182.92.169.111:3306/baiphpwind
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
代码如下:这段代码,在java工程和Android工程是完全一样的。
// TODO Auto-generated method stub
String dbDriver = "com.mysql.jdbc.Driver";
//用jdbc协议来访问mysql数据库,名字是myuser
String url = "jdbc:mysql://182.92.169.111:3306/baiphpwind";
//用户名和密码
String username = "root";
String password = "7eac1c0056";
Statement mStatement = null;
ResultSet mResultSet = null;
Connection mConnection = null;
//查询语句
String sql = "INSERT INTO `baiphpwind`.`pre_wlxx`" +
" (`ddbh` ,`sjrxm` )VALUES ('32','333')";
try{
Class.forName(dbDriver).newInstance();
System.out.print("get 1");
//错在这句了
mConnection = DriverManager.getConnection(url, username, password);
System.out.print("get 2");
mStatement = mConnection.createStatement();
System.out.print("get 3");
int count = mStatement.executeUpdate(sql);// 执行更新操作的sql语句,返回更新数据的个数
try{
System.out.print(count + "\n");
}catch (Exception e){
System.out.println("data error 1\n" + e.getMessage());
}
}catch (SQLException e){
System.out.println("data error 2\n" + url + "\n" + e.getMessage());
}catch (Exception e){
e.printStackTrace();
}finally{
// mStatement.close();
// mConnection.close();
}
------解决思路----------------------
1. 是不是缺少jar包?
2. 为什么要在手机上直接连接mysql;最好自己搞个服务器端,通过服务器去连接
------解决思路----------------------
直接连数据库确实问题多多的,有些手机能成功,有些又不行。而且安全性又差,建议中间再加一层web的
------解决思路----------------------
你上面说getConnection这个方法错了
看下程序有android访问网络的权限么
还有android访问网络操作要在工作线程下进行
------解决思路----------------------
1. 自己搞个web服务器吧,不然你的代码给反编译,你的数据库的密码,全都给别人拿到
2. 很少有人直接连mysql, 这样对手机端,是比较耗电的
quote=引用:]
我在手机端编写app,将采集的数据传到服务器的mysql去。
jar添加了,不然不会出现mysql的错误啊。我在电脑上用java测试,然后移到手机上就出错。
------解决思路----------------------
有错的话可以先看下android那端是如何报错的,排查是android的问题还是其他环境问题等